Welcome to Swift (CocoaCoder 6/12/14)

of 21 /21
Welcome to Swift Carl Brown (@CarlBrwn) CocoaCoder.org, 12 June 2014 (WWDC Keynote + 10 days)

description

Yet another Intro presentation to Apple's new Swift programming language.

Transcript of Welcome to Swift (CocoaCoder 6/12/14)

Page 1: Welcome to Swift (CocoaCoder 6/12/14)

Welcome to SwiftCarl Brown (@CarlBrwn) CocoaCoder.org, 12 June 2014 (WWDC Keynote + 10 days)

Page 2: Welcome to Swift (CocoaCoder 6/12/14)

Disclaimer• We’ve known of this

language’s existence for less than two weeks

• I’ve yet to ship a project with it • We’ve been told the language

will be changing over time

*image: http://vectorgoods.com/wp-content/uploads/2012/01/nuclear-danger-vector.jpg

Page 3: Welcome to Swift (CocoaCoder 6/12/14)

Event InteractivityPlease(!) stop me if you have questions

http://farm3.static.flickr.com/2197/2200500024_e93db99b61.jpg

Page 4: Welcome to Swift (CocoaCoder 6/12/14)

What is Swift?New Programming Language from Apple

Announced at WWDC 2014

After being worked on in secret for 4 years

Interoperates with (and may eventually replace) Objective-C

Currently still a work in progress

Page 5: Welcome to Swift (CocoaCoder 6/12/14)

Looks a lot like a scripting language*

let individualScores = [75, 43, 103, 87, 12] var teamScore = 0 for score in individualScores { if score > 50 { teamScore += 3 } else { teamScore += 1 } } println ("the score is \(teamScore)")

*but it’s compiled

Page 6: Welcome to Swift (CocoaCoder 6/12/14)

Swift != Obj-CGoodbye Square Brackets*

!

…and semicolons

*Except Array definitions and subscripts

Page 7: Welcome to Swift (CocoaCoder 6/12/14)

Standard-ish Types

var int1: Int = 30 var float1: Float = 30.5 var bool1: Bool = false var str1: String = "Hello, playground" var array1: Array = ["Hi", 40] var dict1: Dictionary <String,String> = [ "key" : "value"]

Page 8: Welcome to Swift (CocoaCoder 6/12/14)

Type-safe (and types can be implied)

Page 9: Welcome to Swift (CocoaCoder 6/12/14)

Swift Still has Immutability

let individualScore = 75 !

!

var teamScore = 0

Read-Only

Read-Write

*Array immutability seems complicated for performance reasons

Page 10: Welcome to Swift (CocoaCoder 6/12/14)

Loopsvar firstForLoop = 0 for i in 0..3 { firstForLoop += i }

var n = 2 while n < 100 { n = n * 2 }

var m = 2 do { m = m * 2 } while m < 100

let individualScores = [75, 43, 12] for score in individualScores { teamScore += 3 }

Page 11: Welcome to Swift (CocoaCoder 6/12/14)

Switch directly on Objectslet vegetable = "red pepper" switch vegetable { case "celery": let vegetableComment = "Add some raisins." case "cucumber", "watercress": let vegetableComment = "Make a good tea sandwich." case let x where x.hasSuffix("pepper"): let vegetableComment = "Is it a spicy \(x)?" default: let vegetableComment = "Everything okay in soup." }

*All possibilities MUST be covered or compiler error

Page 12: Welcome to Swift (CocoaCoder 6/12/14)

“Tuples”

let (statusCode, statusMessage) = http404Error let http404Error = (404, "Not Found")

println("The status code is \(statusCode)") // prints "The status code is 404" println("The status message is \(statusMessage)") // prints "The status message is Not Found”

*This Excerpt (and many others) From: Apple Inc. “The Swift Programming Language.” iBooks.

Page 13: Welcome to Swift (CocoaCoder 6/12/14)

Compiler Enforces Initialization

Page 14: Welcome to Swift (CocoaCoder 6/12/14)

“Optionals”Primitive Types can’t be nil

Types have “Optional” versions (nil or value)

You have to “Unwrap” Optionals before you can use them in non-optional context

Page 15: Welcome to Swift (CocoaCoder 6/12/14)

Convention: Use “if let” to “Unwrap”

var optionalString: String? !

if let definiteString = optionalString { println(definiteString) }

*You can also use (!) and risk a run-time crash

Page 16: Welcome to Swift (CocoaCoder 6/12/14)

“Generics”–Strongly Typed Collections

Page 17: Welcome to Swift (CocoaCoder 6/12/14)

Generic Dictionaries, too

Page 18: Welcome to Swift (CocoaCoder 6/12/14)

“Closures”

var strings = ["a","b","c","d"] let uppercaseStrings = strings.map { (s1: String) -> String in return s1.uppercaseString } //["A", "B", "C", "D"]

Page 19: Welcome to Swift (CocoaCoder 6/12/14)

Functions

func getGasPrices() -> (Double, Double, Double) { return (3.59, 3.69, 3.79) } getGasPrices()

*This Excerpt (and many others) From: Apple Inc. “The Swift Programming Language.” iBooks.

Page 20: Welcome to Swift (CocoaCoder 6/12/14)

Classes/Objectsclass NamedShape { var numberOfSides: Int = 0 var name: String init(name: String) { self.name = name } func simpleDescription() -> String { return "A shape with \(numberOfSides) sides." } }

var shape = NamedShape(name: "square") shape.numberOfSides = 4

Page 21: Welcome to Swift (CocoaCoder 6/12/14)

Interoperabilitylet path="/tmp/stringFile.txt" !

let content : AnyObject? = NSString.stringWithContentsOfFile(path) !

if let contentString = content as? String { contentString }