Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Swift Introduction

A Swift Introduction

Apple has introduced a brand-new programming language to the world. Now at 1.0, Swift is here to make your iOS and OS X apps more awesome. Let’s take a look at the language, how it’s used, and play with it! This session assumes no previous experience with Apple platforms or Objective-C, though we will compare Swift code to Objective-C code to see just how much better it can make some programming tasks. Much of this session will take place inside of an iOS Playground in Swift. We will see code live in Apple’s new REPL which was inspired by Light Table.

Jeff Kelley

November 15, 2014
Tweet

More Decks by Jeff Kelley

Other Decks in Programming

Transcript

  1. [Swift] greatly benefited from the experiences hard-won by many other

    languages in the field, drawing ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list. Playgrounds were heavily influenced by Bret Victor's ideas, by Light Table and by many other interactive systems.
  2. Variables var answerToEverything = 42
 var answerToEverything: Int = 42


    let constantAnswer = 42
 let constantAnswer: Double = 42
  3. Variables var answerToEverything: Int = 42
 let constantAnswer: Int =

    42
 answerToEverything += 1 constantAnswer = 43;
  4. Variables var answerToEverything: Int = 42 let constantAnswer: Int =

    42 answerToEverything += 1 constantAnswer = 43;
  5. Functions func reverseString(string: String) -> String { var output =

    "" for character in string { output = character + output } return output }
  6. Functions let myName = "Jeff" func reverseString(string: String) -> String

    { var output = "" for character in string { output = character + output } return output } reverseString(myName) // Outputs "ffeJ"
  7. Functions func printEmojiForAnimals(: Character, : Character, : Character, : Character)

    { println("Dog: \()") println("Cat: \()") println("Horse: \()") println("Cow: \()") } printEmojiForAnimals("", "", "", "")
  8. Functions func printEmojiForAnimals(dog : Character, cat : Character, horse :

    Character, cow : Character) { println("Dog: \()") println("Cat: \()") println("Horse: \()") println("Cow: \()") } printEmojiForAnimals(dog: "", cat: "", horse: "", cow: "")
  9. Functions func printEmojiForAnimals(dog : Character = "", cat : Character

    = "", horse : Character = "", cow : Character = "") { println("Dog: \()") println("Cat: \()") println("Horse: \()") println("Cow: \()") } printEmojiForAnimals(dog: "", cat: "", horse: "", cow: "")
  10. Functions func printEmojiForAnimals(dog : Character = "", cat : Character

    = "", horse : Character = "", cow : Character = "") { println("Dog: \()") println("Cat: \()") println("Horse: \()") println("Cow: \()") } printEmojiForAnimals()
  11. Functions func printEmojiForAnimals(dog : Character = "", cat : Character

    = "", horse : Character = "", cow : Character = "") { println("Dog: \()") println("Cat: \()") println("Horse: \()") println("Cow: \()") } printEmojiForAnimals(cat: "")
  12. Multiple Return Values func bounds(integers: [Int]) -> (min: Int, max:

    Int) { var min = Int.max var max = Int.min for n in integers { if (n < min) { min = n } if (n > max) { max = n } } return (min, max) } var result = bounds([1,2,3,4]) var min = result.min // 1 var max = result.max // 4
  13. Arrays var someObjects = ["foo", "bar", "baz", 42] var someStrings

    = ["foo", "bar", "baz"] someStrings.append(42)
  14. Arrays var someObjects: [AnyObject] = ["foo", "bar", "baz", 42] var

    someStrings: [String] = ["foo", "bar", "baz"] someStrings.append(42)
  15. Arrays var someObjects: [AnyObject] = ["foo", "bar", "baz", 42] var

    someStrings: [String] = ["foo", "bar", "baz"] someStrings.append(42)
  16. Dictionaries var favoriteIDEs = ["Amber": "AppCode", "Jeff": "Xcode"] favoriteIDEs["Nate"] =

    "whatever tool is best for Scala" println("Nate’s favorite IDE is " + favoriteIDEs["Nate"]!) // Outputs “Nate’s favorite IDE is whatever tool is best for Scala”
  17. Dictionaries var favoriteIDEs println // Outputs “Nate’s favorite IDE is

    whatever tool is best for Scala” var favoriteIDEs = ["Amber": "AppCode", "Jeff": "Xcode"] favoriteIDEs["Nate"] = "whatever tool is best for Scala" println("Nate’s favorite IDE is " + favoriteIDEs["Nate"]!) // Outputs “Nate’s favorite IDE is whatever tool is best for Scala”
  18. Optionals var hasPermission: Bool? if (hasPermission != nil) { if

    (hasPermission!) { // Do something with permission } else { // You don’t have permission! } } else { // Ask for permission }
  19. Optionals var hasPermission: Bool? if let givenPermission = hasPermission {

    if (givenPermission) { // Do something with permission } else { // You don’t have permission! } } else { // Ask for permission }
  20. Optionals func logWithOptionalString(string: String? = nil) { if let message

    = string { NSLog("Received message %@", message) } else { NSLog("Logged with no message") } } logWithOptionalString(string: "Hello") logWithOptionalString()
  21. Classes class Animal { var color: NSColor var name: String

    init (color: NSColor, name: String) { self.color = color self.name = name } } class Dog : Animal { var chasesCars: Bool init(color: NSColor, name: String, chasesCars: Bool) { self.chasesCars = chasesCars super.init(color: color, name: name) } func bark() -> () { println("Woof!") } } var Bella = Dog(color: NSColor.whiteColor(), name: "Bella", chasesCars: false)
  22. Classes class Animal { var color: NSColor let name: String

    init (color: NSColor, name: String) { self.color = color self.name = name } } class Dog : Animal { var chasesCars: Bool init(color: NSColor, name: String, chasesCars: Bool) { self.chasesCars = chasesCars super.init(color: color, name: name) } func bark() -> () { println("Woof!") } } var Bella = Dog(color: NSColor.whiteColor(), name: "Bella", chasesCars: false)
  23. Classes class Dog : Animal { var chasesCars: Bool =

    true init(color: NSColor, name: String, chasesCars: Bool) { self.chasesCars = chasesCars super.init(color: color, name: name) } func bark() { println("Woof!") } } class Chihuahua : Dog { override func bark() { println("Yip yip yip yip!") } }
  24. Classes typealias Doge = Dog class Shibe : Doge {

    init(name: String) { super.init(color: NSColor.orangeColor(), name: name, chasesCars: false) } override func bark() { println("wow") } } var superShibe = Shibe(name: "much subclass")
  25. Classes class : Animal { let cares = false }

    var kitty = (color: NSColor.whiteColor(), name: "Kitty Cat");
  26. Extensions extension CGRect { func area() -> CGFloat { return

    CGRectGetWidth(self) * CGRectGetHeight(self) } } var rect = CGRect(x: 100, y: 100, width: 512, height: 512) rect.area() // 262144.0
  27. Structs struct SolarSystem { let star: Star var planets: [Planet]?

    } let ourSystem = SolarSystem(star: sol, planets: [mercury, venus, earth, mars, jupiter, saturn, uranus, neptune])
  28. Structs struct SolarSystem { let star: Star var planets: [Planet]?

    func description() -> String { var description = "The \(star.name) system" if let planets = planets { description += ", orbited by " for i in 0 ..< planets.count { description += planets[i].name if (i + 2 == planets.count) { description += ", and " } else if (i + 1 < planets.count) { description += ", " } } } return description } } let ourSystem = SolarSystem(star: sol, planets: [mercury, venus, earth, mars, jupiter, saturn, uranus, neptune]) ourSystem.description() // The Sol system, orbited by Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune
  29. Structs struct SolarSystem { let star: Star var planets: [Planet]?

    } extension SolarSystem { func description() -> String { var description = "The \(star.name) system" if let planets = planets { description += ", orbited by " for i in 0 ..< planets.count { description += planets[i].name if (i + 2 == planets.count) { description += ", and " } else if (i + 1 < planets.count) { description += ", " } } } return description } } let ourSystem = SolarSystem(star: sol, planets: [mercury, venus, earth, mars, jupiter, saturn, uranus, neptune]) ourSystem.description() // The Sol system, orbited by Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune
  30. Struct Mutability struct SolarSystem { let star: Star var planets:

    [Planet]? } let ourSystem = SolarSystem(star: sol, planets: [mercury, venus, earth, mars, jupiter, saturn, uranus, neptune]) if (ourSystem.planets != nil) { ourSystem.planets!.append(pluto) }
  31. Struct Mutability struct SolarSystem { let star: Star var planets:

    [Planet]? } let ourSystem = SolarSystem(star: sol, planets: [mercury, venus, earth, mars, jupiter, saturn, uranus, neptune]) if (ourSystem.planets != nil) { ourSystem.planets!.append(pluto) }
  32. Struct Mutability struct SolarSystem { let star: Star var planets:

    [Planet]? } var ourSystem = SolarSystem(star: sol, planets: [mercury, venus, earth, mars, jupiter, saturn, uranus, neptune]) if (ourSystem.planets != nil) { ourSystem.planets!.append(pluto) } ourSystem.planets!.count // 9
  33. Classes vs. Structs struct Article { var title: String var

    byline: String var content: String mutating func fixTitleCase() { self.title = self.title.capitalizedString } } class ArticlePreparer { var article: Article init(article: Article) { self.article = article } func prepareForSubmission() { article.fixTitleCase() } } var swiftIsAwesome = Article(title: "Swift is awesome", byline: "by Jeff Kelley", content: " is a valid class name.") var preparer = ArticlePreparer(article: swiftIsAwesome) preparer.prepareForSubmission() swiftIsAwesome.title // "Swift is awesome"
  34. Classes vs. Structs class Article { var title: String var

    byline: String var content: String init(title: String, byline: String, content: String) { self.title = title self.byline = byline self.content = content } func fixTitleCase() { self.title = self.title.capitalizedString } } class ArticlePreparer { var article: Article init(article: Article) { self.article = article } func prepareForSubmission() { article.fixTitleCase() } } var swiftIsAwesome = Article(title: "Swift is awesome", byline: "by Jeff Kelley", content: " is a valid class name.") var preparer = ArticlePreparer(article: swiftIsAwesome) preparer.prepareForSubmission() swiftIsAwesome.title // "Swift Is Awesome"
  35. Classes vs. Structs Classes • Passed by reference • Require

    you to define initializers • Extensible • Can mutate selves inside of functions Structs • Passed by value • Automatic initializers with every variable • Extensible • Must decorate mutating functions
  36. Arrays and Dictionaries are Structs! struct Array<T> : MutableCollectionType, Sliceable

    { typealias Element = T var startIndex: Int { get } var endIndex: Int { get } subscript (index: Int) -> T func generate() -> IndexingGenerator<[T]> typealias SubSlice = Slice<T> subscript (subRange: Range<Int>) -> Slice<T> /// Initialization from an existing buffer does not have "array.init" /// semantics because the caller may retain an alias to buffer. init(_ buffer: _ArrayBuffer<T>) } … struct Dictionary<Key : Hashable, Value> : CollectionType, DictionaryLiteralConvertible { typealias Element = (Key, Value) typealias Index = DictionaryIndex<Key, Value> /// Create a dictionary with at least the given number of /// elements worth of storage. The actual capacity will be the /// smallest power of 2 that's >= `minimumCapacity`. init(minimumCapacity: Int = default) var startIndex: DictionaryIndex<Key, Value> { get } var endIndex: DictionaryIndex<Key, Value> { get } …
  37. Enums enum ArticleType { case Opinion case LongForm case PoliticalCartoon

    case Editorial case LetterToTheEditor case PoemByEECummings } class Article { var title: String var byline: String var content: String let type: ArticleType … } var swiftIsAwesome = Article(title: "Swift is awesome", byline: "by Jeff Kelley", content: " is a valid class name.", type:.Opinion)
  38. Enums class Article { var title: String var byline: String

    var content: String let type: ArticleType func fixTitleCase() { switch type { case .PoemByEECummings: self.title = self.title.lowercaseString default: self.title = self.title.capitalizedString } } }
  39. Enums enum ArticleType { case Opinion case LongForm case PoliticalCartoon([NSImage])

    case Editorial case LetterToTheEditor case PoemByEECummings } extension Article { func fixTitle() { switch type { case .PoliticalCartoon(let images): self.title = self.title.capitalizedString + " (\(images.count) panels)" case .PoemByEECummings: self.title = self.title.lowercaseString default: self.title = self.title.capitalizedString } } }
  40. Generics func mySwap<T>(inout a: T, inout b: T) { var

    c = b b = a a = c } var a = 42 var b = 9_001 mySwap(&a, &b) a // 9001 b // 42
  41. Closures func sortByGravity(p1: Planet, p2: Planet) -> Bool { return

    p1.gravity > p2.gravity } var sortedPlanets = ourSystem.planets!.sorted(sortByGravity)
  42. Closures func > (left: Planet, right: Planet) -> Bool {

    return left.gravity > right.gravity } var sortedPlanets = ourSystem.planets!.sorted { $0 > $1 }
  43. Closures func > (left: Planet, right: Planet) -> Bool {

    return left.gravity > right.gravity } var sortedPlanets = ourSystem.planets!.sorted(>)
  44. Closures var sortedPlanets: [Planet]= ourSystem.planets!.sorted({ (p1: Planet, p2: Planet) ->

    Bool in return p1.gravity > p2.gravity }) var sortedPlanets = ourSystem.planets!.sorted(>)
  45. Objective-C Interoperability • Swift Classes can inherit from Objective-C Classes

    • Objective-C Classes can inherit from Swift Classes • Swift can extend C structs with functions • Swift code can call Objective-C methods • Objective-C code can call Swift functions • Swift code can call C functions (even malloc())