Your SlideShare is downloading. ×
Welcome to Swift (CocoaCoder 6/12/14)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Welcome to Swift (CocoaCoder 6/12/14)

1,361
views

Published on

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

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

Published in: Mobile, Technology

0 Comments
13 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,361
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
129
Comments
0
Likes
13
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Welcome to Swift Carl Brown (@CarlBrwn) CocoaCoder.org, 12 June 2014 (WWDC Keynote + 10 days)
  • 2. 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
  • 3. Event Interactivity Please(!) stop me if you have questions http://farm3.static.flickr.com/2197/2200500024_e93db99b61.jpg
  • 4. 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
  • 5. 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
  • 6. Swift != Obj-C Goodbye Square Brackets* ! …and semicolons *Except Array definitions and subscripts
  • 7. 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"]
  • 8. Type-safe (and types can be implied)
  • 9. Swift Still has Immutability let individualScore = 75 ! ! var teamScore = 0 Read-Only Read-Write *Array immutability seems complicated for performance reasons
  • 10. Loops var 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 }
  • 11. Switch directly on Objects let 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
  • 12. “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.
  • 13. Compiler Enforces Initialization
  • 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
  • 15. 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
  • 16. “Generics”–Strongly Typed Collections
  • 17. Generic Dictionaries, too
  • 18. “Closures” var strings = ["a","b","c","d"] let uppercaseStrings = strings.map { (s1: String) -> String in return s1.uppercaseString } //["A", "B", "C", "D"]
  • 19. 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.
  • 20. Classes/Objects class 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
  • 21. Interoperability let path="/tmp/stringFile.txt" ! let content : AnyObject? = NSString.stringWithContentsOfFile(path) ! if let contentString = content as? String { contentString }

×