Welcome to Swift (CocoaCoder 6/12/14)
-
Upload
carl-brown -
Category
Mobile
-
view
695 -
download
4
Embed Size (px)
description
Transcript of Welcome to Swift (CocoaCoder 6/12/14)

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

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

Event InteractivityPlease(!) stop me if you have questions
http://farm3.static.flickr.com/2197/2200500024_e93db99b61.jpg

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

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

Swift != Obj-CGoodbye Square Brackets*
!
…and semicolons
*Except Array definitions and subscripts

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"]

Type-safe (and types can be implied)

Swift Still has Immutability
let individualScore = 75 !
!
var teamScore = 0
Read-Only
Read-Write
*Array immutability seems complicated for performance reasons

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 }

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

“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.

Compiler Enforces Initialization

“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

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

“Generics”–Strongly Typed Collections

Generic Dictionaries, too

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

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.

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

Interoperabilitylet path="/tmp/stringFile.txt" !
let content : AnyObject? = NSString.stringWithContentsOfFile(path) !
if let contentString = content as? String { contentString }