Intro Swift

Embed Size (px)

Citation preview

  • 8/12/2019 Intro Swift

    1/343

    2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

    #WWD

    Introduction to Swift

    Session 402

    Tim IstedDeveloper Publications Engineer

    Tools

    Dave AddeyDeveloper Publications Engineer

  • 8/12/2019 Intro Swift

    2/343

  • 8/12/2019 Intro Swift

    3/343

  • 8/12/2019 Intro Swift

    4/343

    370,000iBooks Store downloads

  • 8/12/2019 Intro Swift

    5/343

    #include

    int main()

    {

    printf("hello,world\n");return 0;

    }

  • 8/12/2019 Intro Swift

    6/343

    println("hello,WWDC")

  • 8/12/2019 Intro Swift

    7/343

    What You Will Learn

  • 8/12/2019 Intro Swift

    8/343

    What You Will Learn

  • 8/12/2019 Intro Swift

    9/343

    What You Will Learn

  • 8/12/2019 Intro Swift

    10/343

    What You Will Learn

  • 8/12/2019 Intro Swift

    11/343

    The Basics

    Dave AddeyDeveloper Publications Engineer

  • 8/12/2019 Intro Swift

    12/343

    Variables

  • 8/12/2019 Intro Swift

    13/343

    Variables

    var

  • 8/12/2019 Intro Swift

    14/343

  • 8/12/2019 Intro Swift

    15/343

    var languageName:

    Variables

  • 8/12/2019 Intro Swift

    16/343

    var languageName: String

    Variables

  • 8/12/2019 Intro Swift

    17/343

    var languageName: String = "Swift"

    Variables

  • 8/12/2019 Intro Swift

    18/343

    letlanguageName: String = "Swift"

    Variables

  • 8/12/2019 Intro Swift

    19/343

    Constants and

    letlanguageName: String = "Swift"

    Variables

  • 8/12/2019 Intro Swift

    20/343

    Constants and

    letlanguageName: String = "Swift"

    var version: Double = 1.0

    Variables

  • 8/12/2019 Intro Swift

    21/343

    Constants and

    letlanguageName: String = "Swift"

    var version: Double = 1.0

    var introduced: Int = 2014

    Variables

  • 8/12/2019 Intro Swift

    22/343

    Constants and

    letlanguageName: String = "Swift"

    var version: Double = 1.0

    var introduced: Int = 2014

    var isAwesome: Bool = true

    Variables

  • 8/12/2019 Intro Swift

    23/343

    Constants and

    letlanguageName: String = "Swift"

    var version: Double = 1.0

    letintroduced: Int = 2014

    letisAwesome: Bool = true

    Variables

  • 8/12/2019 Intro Swift

    24/343

    Constants and Variables

    let languageName: String= "Swift"

    var version: Double= 1.0

    let introduced: Int= 2014

    let isAwesome: Bool= true

  • 8/12/2019 Intro Swift

    25/343

    Constants and Variables

    let languageName: String = "Swift"

    var version: Double = 1.0

    let introduced: Int = 2014

    let isAwesome: Bool = true

    f

  • 8/12/2019 Intro Swift

    26/343

    Type Inference

    let languageName = "Swift" // inferred as String

    var version = 1.0 // inferred as Double

    let introduced = 2014 // inferred as Int

    let isAwesome = true // inferred as Bool

    U i d N

  • 8/12/2019 Intro Swift

    27/343

    let languageName = "Swift"

    var version = 1.0

    let introduced = 2014

    let isAwesome = true

    let "= 3.1415927

    Unicode Names

    U i d N

  • 8/12/2019 Intro Swift

    28/343

    let languageName = "Swift"

    var version = 1.0

    let introduced = 2014

    let isAwesome = true

    let "= 3.1415927

    let !"= "dogcow"

    Unicode Names

    St i

  • 8/12/2019 Intro Swift

    29/343

    String

    St i

  • 8/12/2019 Intro Swift

    30/343

    let someString = "I appear to be a string"

    String

    St i

  • 8/12/2019 Intro Swift

    31/343

    let someString = "I appear to be a string"

    // inferred to be of type String

    String

    St i

  • 8/12/2019 Intro Swift

    32/343

    let someString = "I appear to be a string"

    // inferred to be of type String

    urlRequest.HTTPMethod = "POST"

    String

    String

  • 8/12/2019 Intro Swift

    33/343

    let someString = "I appear to be a string"// inferred to be of type String

    urlRequest.HTTPMethod = "POST"

    let components = "~/Documents/Swift".pathComponents

    String

    String

  • 8/12/2019 Intro Swift

    34/343

    let someString = "I appear to be a string"// inferred to be of type String

    urlRequest.HTTPMethod = "POST"

    let components = "~/Documents/Swift".pathComponents// ["~", "Documents", "Swift"]

    String

    Character

  • 8/12/2019 Intro Swift

    35/343

    Character

    Character

  • 8/12/2019 Intro Swift

    36/343

    for character in "mouse"{println(character)

    }

    Character

    Character

  • 8/12/2019 Intro Swift

    37/343

    for character in "mouse"{println(character)

    }

    Character

    m

    o

    u

    s

    e

    Character

  • 8/12/2019 Intro Swift

    38/343

    for character in "ms"{println(character)

    }

    Character

    m

    s

    Character

  • 8/12/2019 Intro Swift

    39/343

    for character in "#$%&"{println(character)

    }

    Character

    #

    $

    %

    &

    Character

  • 8/12/2019 Intro Swift

    40/343

    for character in ""{

    Character

    println(character)

    }

    Character

  • 8/12/2019 Intro Swift

    41/343

    println(character)

    }

    for character in "#####"{

    Character

    #

    #

    #

    #

    #

    Combining Strings and Characters

  • 8/12/2019 Intro Swift

    42/343

    let dog: Character = "!"

    Combining Strings and Characters

    Combining Strings and Characters

  • 8/12/2019 Intro Swift

    43/343

    let dog: Character = "!"

    let cow: Character = """

    Combining Strings and Characters

    Combining Strings and Characters

  • 8/12/2019 Intro Swift

    44/343

    let dog: Character = "!"

    let cow: Character = """

    let dogCow = dog + cow

    // dogCow is "!""

    Combining Strings and Characters

    Combining Strings and Characters

  • 8/12/2019 Intro Swift

    45/343

    let dog: Character = "!"

    let cow: Character = """

    let dogCow = dog + cow

    // dogCow is "!""

    let instruction = "Beware of the " + dog

    // instruction is "Beware of the !"

    Combining Strings and Characters

    Building Complex Strings

  • 8/12/2019 Intro Swift

    46/343

    Building Complex Strings

    Building Complex Strings

  • 8/12/2019 Intro Swift

    47/343

    Building Complex Strings

    let a = 3, b = 5

    Building Complex Strings

  • 8/12/2019 Intro Swift

    48/343

    let a = 3, b = 5

    // "3 times 5 is 15"

    g p g

    String Interpolation

  • 8/12/2019 Intro Swift

    49/343

    let a = 3, b = 5

    // "3 times 5 is 15"

    let mathResult = "\(a)times \(b)is \(a * b)"

    g p

    String Interpolation

  • 8/12/2019 Intro Swift

    50/343

    let a = 3, b = 5

    // "3 times 5 is 15"

    let mathResult = "\(a)times \(b)is \(a * b)"

    // "3 times 5 is 15"

    g p

    String Interpolation

  • 8/12/2019 Intro Swift

    51/343

    let a = 7, b = 4

    // "7 times 4 is 28"

    let mathResult = "\(a)times \(b)is \(a * b)"

    // "7 times 4 is 28"

    g p

    String Mutability

  • 8/12/2019 Intro Swift

    52/343

    g y

    String Mutability

  • 8/12/2019 Intro Swift

    53/343

    var variableString = "Horse"

    g y

    String Mutability

  • 8/12/2019 Intro Swift

    54/343

    var variableString = "Horse"variableString += " and carriage"

    g y

    String Mutability

  • 8/12/2019 Intro Swift

    55/343

    var variableString = "Horse"variableString += " and carriage"

    // variableString is now "Horse and carriage"

    String Mutability

  • 8/12/2019 Intro Swift

    56/343

    var variableString = "Horse"variableString += " and carriage"

    // variableString is now "Horse and carriage"

    let constantString = "Highlander"

    String Mutability

  • 8/12/2019 Intro Swift

    57/343

    var variableString = "Horse"variableString += " and carriage"

    // variableString is now "Horse and carriage"

    let constantString = "Highlander"

    constantString += " and another Highlander"

    String Mutability

  • 8/12/2019 Intro Swift

    58/343

    var variableString = "Horse"variableString += " and carriage"

    // variableString is now "Horse and carriage"

    let constantString = "Highlander"

    constantString += " and another Highlander"// error - constantString cannot be changed

    Array and Dictionary

  • 8/12/2019 Intro Swift

    59/343

    Array and Dictionary

  • 8/12/2019 Intro Swift

    60/343

    let components = "~/Documents/Swift".pathComponents

    Array and Dictionary

  • 8/12/2019 Intro Swift

    61/343

    let components = "~/Documents/Swift".pathComponents// ["~", "Documents", "Swift"]

    // returns an Array, not an NSArray

    Array and Dictionary Literals

  • 8/12/2019 Intro Swift

    62/343

    Array and Dictionary Literals

  • 8/12/2019 Intro Swift

    63/343

    var names = ["Anna", "Alex", "Brian", "Jack"]

    Array and Dictionary Literals

  • 8/12/2019 Intro Swift

    64/343

    var names = ["Anna", "Alex", "Brian", "Jack"]

    Array and Dictionary Literals

  • 8/12/2019 Intro Swift

    65/343

    var names = ["Anna", "Alex", "Brian", "Jack"]

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    Array and Dictionary Literals

  • 8/12/2019 Intro Swift

    66/343

    var names = ["Anna", "Alex", "Brian", "Jack"]

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    Array and Dictionary Literals

  • 8/12/2019 Intro Swift

    67/343

    var names = ["Anna", "Alex", "Brian", "Jack"]

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    Arrays and Dictionaries

  • 8/12/2019 Intro Swift

    68/343

    var names = ["Anna", "Alex", "Brian", "Jack"]

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    Arrays and Dictionaries

  • 8/12/2019 Intro Swift

    69/343

    var names = ["Anna", "Alex", "Brian", "Jack"]

    var numberOfLegs = ["ant":6, "snake":0, "cheetah":4]

    Typed Collections

  • 8/12/2019 Intro Swift

    70/343

    var names = ["Anna", "Alex", "Brian", "Jack"]

    Typed Collections

  • 8/12/2019 Intro Swift

    71/343

    var names = ["Anna", "Alex", "Brian", "Jack"]

    Typed Collections

  • 8/12/2019 Intro Swift

    72/343

    var names = ["Anna", "Alex", "Brian", "Jack", 42]

    Typed Collections

  • 8/12/2019 Intro Swift

    73/343

    var names = ["Anna", "Alex", "Brian", "Jack", true]

    Typed Collections

  • 8/12/2019 Intro Swift

    74/343

    var names = ["Anna", "Alex", "Brian", "Jack", Bicycle()

    Typed Collections

  • 8/12/2019 Intro Swift

    75/343

    var names = ["Anna", "Alex", "Brian", "Jack"]

    Typed Collections

  • 8/12/2019 Intro Swift

    76/343

    var names: String[]= ["Anna", "Alex", "Brian", "Jack"]

    Typed Collections

  • 8/12/2019 Intro Swift

    77/343

    var names: String[] = ["Anna", "Alex", "Brian", "Jack"]

    Typed Collections

  • 8/12/2019 Intro Swift

    78/343

    var names:String[]= ["Anna", "Alex", "Brian", "Jack"]

    Typed Collections

  • 8/12/2019 Intro Swift

    79/343

    var names = ["Anna", "Alex", "Brian", "Jack"]// an array of String values

    Typed Collections

  • 8/12/2019 Intro Swift

    80/343

    var names = ["Anna", "Alex", "Brian", "Jack"]// an array of String values

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    Typed Collections

  • 8/12/2019 Intro Swift

    81/343

    var names = ["Anna", "Alex", "Brian", "Jack"]// an array of String values

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    Typed Collections

  • 8/12/2019 Intro Swift

    82/343

    var names = ["Anna", "Alex", "Brian", "Jack"]// an array of String values

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    // a Dictionary with String keys and Int values

    Typed Collections

  • 8/12/2019 Intro Swift

    83/343

    var names = ["Anna", "Alex", "Brian", "Jack"]// an array of String values

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    // a Dictionary with String keys and Int values

    Loops

  • 8/12/2019 Intro Swift

    84/343

    Loops

  • 8/12/2019 Intro Swift

    85/343

    while !sated {

    eatCake()

    }

    for var doctor = 1; doctor

  • 8/12/2019 Intro Swift

    86/343

    #

    #

    #

    #

    #

    for character in "#####"{

    println(character)

    }

  • 8/12/2019 Intro Swift

    87/343

    For-In: Ranges

  • 8/12/2019 Intro Swift

    88/343

    for number in 0..5{

    println("\(number) times 4 is \(number * 4)")

    }

    0 times 4 is 0

    1 times 4 is 4

    2 times 4 is 8

    3 times 4 is 12

    4 times 4 is 16

    For-In: Arrays

  • 8/12/2019 Intro Swift

    89/343

    for name in ["Anna", "Alex", "Brian", "Jack"]{

    println("Hello, \(name)!")

    }

    Hello, Anna!

    Hello, Alex!

    Hello, Brian!

    Hello, Jack!

    For-In: Dictionaries

  • 8/12/2019 Intro Swift

    90/343

    let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    for (animalName, legCount)in numberOfLegs {

    println("\(animalName)s have \(legCount) legs")

    }

    ants have 6 legs

    snakes have 0 legs

    cheetahs have 4 legs

    For-In: Dictionaries

  • 8/12/2019 Intro Swift

    91/343

    let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    for (animalName, legCount)in numberOfLegs {

    println("\(animalName)s have \(legCount) legs")

    }

    ants have 6 legs

    snakes have 0 legs

    cheetahs have 4 legs

    Modifying an Array

  • 8/12/2019 Intro Swift

    92/343

    Modifying an Array

  • 8/12/2019 Intro Swift

    93/343

    var shoppingList = ["Eggs", "Milk"]

    Modifying an Array

  • 8/12/2019 Intro Swift

    94/343

    var shoppingList = ["Eggs", "Milk"]

    ["Eggs", "Milk"]

    Modifying an Array

  • 8/12/2019 Intro Swift

    95/343

    var shoppingList = ["Eggs", "Milk"]

    println(shoppingList[0])

    Modifying an Array

  • 8/12/2019 Intro Swift

    96/343

    var shoppingList = ["Eggs", "Milk"]

    println(shoppingList[0])

    "Eggs"

    Modifying an Array

  • 8/12/2019 Intro Swift

    97/343

    var shoppingList = ["Eggs", "Milk"]

    println(shoppingList[0])

    shoppingList += "Flour"

  • 8/12/2019 Intro Swift

    98/343

    Modifying an Array

  • 8/12/2019 Intro Swift

    99/343

    var shoppingList = ["Eggs", "Milk"]

    println(shoppingList[0])

    shoppingList += "Flour"

    shoppingList += ["Cheese", "Butter", "Chocolate Spread"

    Modifying an Array

  • 8/12/2019 Intro Swift

    100/343

    var shoppingList = ["Eggs", "Milk"]

    println(shoppingList[0])

    shoppingList += "Flour"

    shoppingList += ["Cheese", "Butter", "Chocolate Spread"

    ["Eggs", "Milk", "Flour", "Cheese", "Butter",

    "Chocolate Spread"]

    Modifying an Array

  • 8/12/2019 Intro Swift

    101/343

    var shoppingList = ["Eggs", "Milk"]

    println(shoppingList[0])

    shoppingList += "Flour"

    shoppingList += ["Cheese", "Butter", "Chocolate Spread"

    shoppingList[0] = "Six eggs"

    ["Eggs", "Milk", "Flour", "Cheese", "Butter",

    "Chocolate Spread"]

  • 8/12/2019 Intro Swift

    102/343

    Modifying an Array

  • 8/12/2019 Intro Swift

    103/343

    var shoppingList = ["Eggs", "Milk"]

    println(shoppingList[0])

    shoppingList += "Flour"

    shoppingList += ["Cheese", "Butter", "Chocolate Spread"

    shoppingList[0] = "Six eggs"

    shoppingList[3...5] = ["Bananas", "Apples"]

    ["Six eggs", "Milk", "Flour", "Cheese", "Butter",

    "Chocolate Spread"]

    Modifying an Array

  • 8/12/2019 Intro Swift

    104/343

    var shoppingList = ["Eggs", "Milk"]

    println(shoppingList[0])

    shoppingList += "Flour"

    shoppingList += ["Cheese", "Butter", "Chocolate Spread"

    shoppingList[0] = "Six eggs"

    shoppingList[3...5] = ["Bananas", "Apples"]

    ["Six eggs", "Milk", "Flour", "Bananas", "Apples"]

    Modifying a Dictionary

  • 8/12/2019 Intro Swift

    105/343

    Modifying a Dictionary

  • 8/12/2019 Intro Swift

    106/343

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

  • 8/12/2019 Intro Swift

    107/343

  • 8/12/2019 Intro Swift

    108/343

  • 8/12/2019 Intro Swift

    109/343

    Modifying a Dictionary

  • 8/12/2019 Intro Swift

    110/343

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    numberOfLegs["spider"] = 273

    numberOfLegs["spider"] = 8

    ["ant": 6, "snake": 0, "cheetah": 4, "spider": 273]

    Modifying a Dictionary

  • 8/12/2019 Intro Swift

    111/343

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    numberOfLegs["spider"] = 273

    numberOfLegs["spider"] = 8

    ["ant": 6, "snake": 0, "cheetah": 4, "spider": 8]

  • 8/12/2019 Intro Swift

    112/343

    Retrieving a Value from a Dictionary

  • 8/12/2019 Intro Swift

    113/343

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    // aardvark?

    Retrieving a Value from a Dictionary

  • 8/12/2019 Intro Swift

    114/343

    var numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    // aardvark?// dugong?

  • 8/12/2019 Intro Swift

    115/343

  • 8/12/2019 Intro Swift

    116/343

    Beyond the Basics

    Tim IstedDeveloper Publications Engineer

  • 8/12/2019 Intro Swift

    117/343

    Optionals

  • 8/12/2019 Intro Swift

    118/343

    let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    let possibleLegCount: Int?= numberOfLegs["aardvark"]

    Querying an Optional

  • 8/12/2019 Intro Swift

    119/343

    let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    let possibleLegCount: Int? = numberOfLegs["aardvark"]

    if possibleLegCount == nil{

    println("Aardvark wasn't found")

    }

  • 8/12/2019 Intro Swift

    120/343

  • 8/12/2019 Intro Swift

    121/343

    if ibl L C t {

    Querying an Optional

  • 8/12/2019 Intro Swift

    122/343

    if possibleLegCount{

    let legCount = possibleLegCount!

    println("An aardvark has \(legCount) legs")

    }

  • 8/12/2019 Intro Swift

    123/343

    if let legCo nt possibleLegCo nt {

    Unwrapping an Optional

  • 8/12/2019 Intro Swift

    124/343

    if let legCount = possibleLegCount{

    println("An aardvark has \(legCount) legs")}

  • 8/12/2019 Intro Swift

    125/343

  • 8/12/2019 Intro Swift

    126/343

    If Statements

    if legCount == 0 {

  • 8/12/2019 Intro Swift

    127/343

    if legCount == 0 {

    println("It slithers and slides around")} else {

    println("It walks")

    }

    If Statements

    if legCount == 0 {

  • 8/12/2019 Intro Swift

    128/343

    if legCount == 0 {

    println("It slithers and slides around")}else {

    println("It walks")

    }

    More Complex If Statements

    if legCount == 0 {

  • 8/12/2019 Intro Swift

    129/343

    iflegCount == 0 {

    println("It slithers and slides around")} else if legCount == 1{

    println("It hops")

    } else {

    println("It walks")

    }

  • 8/12/2019 Intro Swift

    130/343

  • 8/12/2019 Intro Swift

    131/343

    Switch

    switch legCount {

  • 8/12/2019 Intro Swift

    132/343

    switch legCount {

    case 0:println("It slithers and slides around")

    case 1, 3, 5, 7, 9, 11, 13:

    println("It limps")

    case 2, 4, 6, 8, 10, 12, 14:

    println("It walks")

    }

  • 8/12/2019 Intro Swift

    133/343

  • 8/12/2019 Intro Swift

    134/343

  • 8/12/2019 Intro Swift

    135/343

  • 8/12/2019 Intro Swift

    136/343

    Matching Value Ranges

    switch legCount {

  • 8/12/2019 Intro Swift

    137/343

    case 0:println("It has no legs")

    case 1...8:

    println("It has a few legs")

    default:

    println("It has lots of legs")

    }

  • 8/12/2019 Intro Swift

    138/343

  • 8/12/2019 Intro Swift

    139/343

  • 8/12/2019 Intro Swift

    140/343

  • 8/12/2019 Intro Swift

    141/343

  • 8/12/2019 Intro Swift

    142/343

  • 8/12/2019 Intro Swift

    143/343

    Functions with Parameters

    func sayHello(name: String) {

  • 8/12/2019 Intro Swift

    144/343

    println("Hello \(name)!")}

    sayHello("WWDC")

    Functions with Parameters

    func sayHello(name: String) {

  • 8/12/2019 Intro Swift

    145/343

    println("Hello \(name)!")}

    sayHello("WWDC")

    Hello WWDC!

  • 8/12/2019 Intro Swift

    146/343

    Default Parameter Values

    func sayHello(name: String = "World") {

  • 8/12/2019 Intro Swift

    147/343

    println("Hello \(name)!")}

    sayHello()

  • 8/12/2019 Intro Swift

    148/343

    Default Parameter Values

    func sayHello(name: String = "World") {

  • 8/12/2019 Intro Swift

    149/343

    println("Hello \(name)!")}

    sayHello()

    sayHello(name: "WWDC")

    Hello World!

  • 8/12/2019 Intro Swift

    150/343

  • 8/12/2019 Intro Swift

    151/343

    Returning Values

    func buildGreeting(name: String = "World") -> String {

  • 8/12/2019 Intro Swift

    152/343

    return "Hello " + name}

    let greeting = buildGreeting()

  • 8/12/2019 Intro Swift

    153/343

  • 8/12/2019 Intro Swift

    154/343

  • 8/12/2019 Intro Swift

    155/343

  • 8/12/2019 Intro Swift

    156/343

  • 8/12/2019 Intro Swift

    157/343

    Tuples

    (3.79, 3.99, 4.19)

  • 8/12/2019 Intro Swift

    158/343

    (404, "Not found")

    (2, "banana", 0.72)

    Tuples

    (3.79, 3.99, 4.19) // (Double, Double, Double)

  • 8/12/2019 Intro Swift

    159/343

    (404, "Not found") // (Int, String)

    (2, "banana", 0.72) // (Int, String, Double)

  • 8/12/2019 Intro Swift

    160/343

  • 8/12/2019 Intro Swift

    161/343

  • 8/12/2019 Intro Swift

    162/343

  • 8/12/2019 Intro Swift

    163/343

  • 8/12/2019 Intro Swift

    164/343

    Tuple Decomposition for Enumeration

    let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4]

    ( ) {

  • 8/12/2019 Intro Swift

    165/343

    for (animalName, legCount)in numberOfLegs {

    println("\(animalName)s have \(legCount) legs")

    }

    ants have 6 legs

    snakes have 0 legs

    cheetahs have 4 legs

    Named Values in a Tuple

    func refreshWebPage() -> (Int, String) {

    // ...try to refresh...

    (200 "S ")

  • 8/12/2019 Intro Swift

    166/343

    return (200, "Success")

    }

  • 8/12/2019 Intro Swift

    167/343

  • 8/12/2019 Intro Swift

    168/343

    Named Values in a Tuple

    func refreshWebPage() -> (code: Int, message: String) {

    // ...try to refresh...

    return (200 "Success")

  • 8/12/2019 Intro Swift

    169/343

    return (200, "Success")

    }

    let status = refreshWebPage()

    println("Received \(status.code): \(status.message)")

    Received 200: Success

  • 8/12/2019 Intro Swift

    170/343

  • 8/12/2019 Intro Swift

    171/343

  • 8/12/2019 Intro Swift

    172/343

    Closures

    let greetingPrinter: () -> ()= {

    println("Hello World!")

    }

  • 8/12/2019 Intro Swift

    173/343

    }

    func greetingPrinter() -> (){

    println("Hello World!")

    }

  • 8/12/2019 Intro Swift

    174/343

  • 8/12/2019 Intro Swift

    175/343

    Closures as Parameters

    func repeat(count: Int, task: () -> ()) {

    for i in 0..count {

    task()}

  • 8/12/2019 Intro Swift

    176/343

    ()}

    }

  • 8/12/2019 Intro Swift

    177/343

    Closures as Parameters

    func repeat(count: Int, task: () -> ()) {

    for i in 0..count {

    task()}

  • 8/12/2019 Intro Swift

    178/343

    }

    }

    repeat(2, {

    println("Hello!")

    })Hello!

    Hello!

  • 8/12/2019 Intro Swift

    179/343

    Trailing Closures

    func repeat(count: Int, task: () -> ()) {

    for i in 0..count {

    task()}

  • 8/12/2019 Intro Swift

    180/343

    }

    }

    repeat(2) {

    println("Hello!")

    }Hello!

    Hello!

    Cl

  • 8/12/2019 Intro Swift

    181/343

    Classes

    Dave AddeyDeveloper Publications Engineer

  • 8/12/2019 Intro Swift

    182/343

  • 8/12/2019 Intro Swift

    183/343

    Classes

    class Vehicle{

  • 8/12/2019 Intro Swift

    184/343

    }

    Classes

    class Vehicle {

  • 8/12/2019 Intro Swift

    185/343

    }

    Classes

    class Vehicle {

    // properties

  • 8/12/2019 Intro Swift

    186/343

    }

  • 8/12/2019 Intro Swift

    187/343

  • 8/12/2019 Intro Swift

    188/343

    Classes

    import "Vehicle.h"

    class Vehicle {

  • 8/12/2019 Intro Swift

    189/343

    }

    Classes

    import "Vehicle.h"

    class Vehicle {

  • 8/12/2019 Intro Swift

    190/343

    }

  • 8/12/2019 Intro Swift

    191/343

  • 8/12/2019 Intro Swift

    192/343

    Classes

    class Vehicle: ????????{

    }

  • 8/12/2019 Intro Swift

    193/343

    Classes

    class Vehicle: NSObject{

    }

  • 8/12/2019 Intro Swift

    194/343

    Classes

    class Vehicle {

    }

  • 8/12/2019 Intro Swift

    195/343

    Class Inheritance

    class Vehicle {

    }

  • 8/12/2019 Intro Swift

    196/343

    class Bicycle: Vehicle {

    }

    Class Inheritance

    class Vehicle {

    }

  • 8/12/2019 Intro Swift

    197/343

    class Bicycle: Vehicle {

    }

    Class Inheritance

    class Vehicle {

    }

  • 8/12/2019 Intro Swift

    198/343

    class Bicycle: Vehicle{

    }

    Class Inheritance

    class Vehicle {

    }

  • 8/12/2019 Intro Swift

    199/343

    Properties

    class Vehicle {

    var numberOfWheels = 0

    }

  • 8/12/2019 Intro Swift

    200/343

    Properties

    class Vehicle {

    varnumberOfWheels = 0

    }

  • 8/12/2019 Intro Swift

    201/343

    Properties

    class Vehicle {

    letnumberOfWheels = 0

    }

  • 8/12/2019 Intro Swift

    202/343

    Properties

    class Vehicle {

    varnumberOfWheels = 0

    }

  • 8/12/2019 Intro Swift

    203/343

    Properties

    class Vehicle {

    var numberOfWheels = 0

    }

  • 8/12/2019 Intro Swift

    204/343

    Properties

    class Vehicle {

    var numberOfWheels = 0

    }

  • 8/12/2019 Intro Swift

    205/343

    PropertiesStored

    class Vehicle {

    var numberOfWheels = 0

    }

  • 8/12/2019 Intro Swift

    206/343

    Computed Properties

  • 8/12/2019 Intro Swift

    207/343

    Computed Properties

    class Vehicle {

    var numberOfWheels = 0

    var description: String {get {

  • 8/12/2019 Intro Swift

    208/343

    return "\(numberOfWheels) wheels"

    }

    }

    }

    Computed Properties

    class Vehicle {

    var numberOfWheels = 0

    var description: String{get {

    ( )

  • 8/12/2019 Intro Swift

    209/343

    return "\(numberOfWheels) wheels"

    }

    }

    }

    Computed Properties

    class Vehicle {

    var numberOfWheels = 0

    var description: String {get {

    " ( b f h l ) h l "

  • 8/12/2019 Intro Swift

    210/343

    return "\(numberOfWheels) wheels"

    }

    }

    }

  • 8/12/2019 Intro Swift

    211/343

    Computed Properties

    class Vehicle {

    var numberOfWheels = 0

    var description: String {get {

    t "\( b OfWh l ) h l "

  • 8/12/2019 Intro Swift

    212/343

    return "\(numberOfWheels) wheels"

    }

    set {

    }}

    }

    Computed Properties

    class Vehicle {

    var numberOfWheels = 0

    var description: String {get {

    ret rn "\(n mberOfWheels) heels"

  • 8/12/2019 Intro Swift

    213/343

    return "\(numberOfWheels) wheels"

    }

    }

    }

    Computed Properties

    class Vehicle {

    var numberOfWheels = 0

    var description: String {return "\(numberOfWheels) wheels"

    }

  • 8/12/2019 Intro Swift

    214/343

    }

    }

    Computed Properties

    class Vehicle {

    var numberOfWheels = 0

    var description: String {return "\(numberOfWheels) wheels"

    }

  • 8/12/2019 Intro Swift

    215/343

    }

    }

    Initializer Syntax

    class Vehicle {

    var numberOfWheels = 0

    var description: String {return "\(numberOfWheels) wheels"

    }

  • 8/12/2019 Intro Swift

    216/343

    }

    }

    let someVehicle = Vehicle()

    class Vehicle {

    var numberOfWheels = 0

    var description: String {return "\(numberOfWheels) wheels"

    }

    Initializer Syntax

  • 8/12/2019 Intro Swift

    217/343

    }

    }

    let someVehicle = Vehicle()

    class Vehicle {

    var numberOfWheels = 0

    var description: String {return "\(numberOfWheels) wheels"

    }

    Automatic Memory Allocation

  • 8/12/2019 Intro Swift

    218/343

    }

    }

    let someVehicle = Vehicle()

    Type Inference

    class Vehicle {

    var numberOfWheels = 0

    var description: String {return "\(numberOfWheels) wheels"

    }

  • 8/12/2019 Intro Swift

    219/343

    }

    }

    let someVehicle: Vehicle= Vehicle()

    Default Values

    class Vehicle {

    var numberOfWheels = 0

    var description: String {return "\(numberOfWheels) wheels"

    }

  • 8/12/2019 Intro Swift

    220/343

    }

    }

    let someVehicle = Vehicle()

    Dot Syntax

    let someVehicle = Vehicle()

  • 8/12/2019 Intro Swift

    221/343

    Dot Syntax

    let someVehicle = Vehicle()

    println(someVehicle.description)

  • 8/12/2019 Intro Swift

    222/343

    Dot Syntax

    let someVehicle = Vehicle()

    println(someVehicle.description)// 0 wheels

  • 8/12/2019 Intro Swift

    223/343

    Dot Syntax

    let someVehicle = Vehicle()

    println(someVehicle.description)// 0 wheels

  • 8/12/2019 Intro Swift

    224/343

    someVehicle.numberOfWheels = 2

    Dot Syntax

    let someVehicle = Vehicle()

    println(someVehicle.description)// 0 wheels

  • 8/12/2019 Intro Swift

    225/343

    someVehicle.numberOfWheels = 2

    println(someVehicle.description)

    Dot Syntax

    let someVehicle = Vehicle()

    println(someVehicle.description)// 0 wheels

  • 8/12/2019 Intro Swift

    226/343

    someVehicle.numberOfWheels = 2

    println(someVehicle.description)// 2 wheels

    class Bicycle: Vehicle {

    Class Initialization

  • 8/12/2019 Intro Swift

    227/343

    }

    Class Initialization

    class Bicycle: Vehicle {

    init() {

    }

  • 8/12/2019 Intro Swift

    228/343

    }

    Class Initialization

    class Bicycle: Vehicle {

    init(){

    }

  • 8/12/2019 Intro Swift

    229/343

    }

    Class Initialization

    class Bicycle: Vehicle {

    init() {

    super.init()

    }

  • 8/12/2019 Intro Swift

    230/343

    }

    Class Initialization

    class Bicycle: Vehicle {

    init() {

    super.init()numberOfWheels = 2

    }

  • 8/12/2019 Intro Swift

    231/343

    }

    Class Initialization

    class Bicycle: Vehicle {

    init() {

    super.init()numberOfWheels = 2

    }

    }

  • 8/12/2019 Intro Swift

    232/343

    }

    Class Initialization

    class Bicycle: Vehicle {

    init() {

    super.init()numberOfWheels = 2

    }

    }

  • 8/12/2019 Intro Swift

    233/343

    }

    let myBicycle = Bicycle()

    Class Initialization

    class Bicycle: Vehicle {

    init() {

    super.init()numberOfWheels = 2

    }

    }

  • 8/12/2019 Intro Swift

    234/343

    }

    let myBicycle = Bicycle()println(myBicycle.description)

    Class Initialization

    class Bicycle: Vehicle {

    init() {

    super.init()numberOfWheels = 2

    }

    }

  • 8/12/2019 Intro Swift

    235/343

    }

    let myBicycle = Bicycle()println(myBicycle.description)

    // 2 wheels

    Overriding a Property

  • 8/12/2019 Intro Swift

    236/343

    Overriding a Property

    class Car: Vehicle {

  • 8/12/2019 Intro Swift

    237/343

    }

    Overriding a Property

    class Car: Vehicle {

    var speed = 0.0 // inferred as Double

  • 8/12/2019 Intro Swift

    238/343

    }

    Overriding a Property

    class Car: Vehicle {

    var speed = 0.0

    init() {super.init()

    numberOfWheels = 4

    }

  • 8/12/2019 Intro Swift

    239/343

    }

    }

    Overriding a Property

    class Car: Vehicle {

    var speed = 0.0

    init() {super.init()

    numberOfWheels = 4

    }

  • 8/12/2019 Intro Swift

    240/343

    }

    var description: String {

    }

    }

    Overriding a Property

    class Car: Vehicle {

    var speed = 0.0

    init() {super.init()

    numberOfWheels = 4

    }

  • 8/12/2019 Intro Swift

    241/343

    }

    overridevar description: String {

    }

    }

    Overriding a Property

    class Car: Vehicle {

    var speed = 0.0

    init() {super.init()

    numberOfWheels = 4

    }

  • 8/12/2019 Intro Swift

    242/343

    }

    overridevar description: String {

    }

    }

    Overriding a Property

    class Car: Vehicle {

    var speed = 0.0

    init() {super.init()

    numberOfWheels = 4

    }

  • 8/12/2019 Intro Swift

    243/343

    }

    override var description: String {

    return super.description+ ", \(speed) mph"}

    }

    Overriding a Property

    class Car: Vehicle {

    var speed = 0.0

    init() {

    super.init()

    numberOfWheels = 4

    }

  • 8/12/2019 Intro Swift

    244/343

    }

    override var description: String {

    return super.description+ ", \(speed) mph"}

    }

    Overriding a Property

    let myCar = Car()

  • 8/12/2019 Intro Swift

    245/343

    Overriding a Property

    let myCar = Car()

    println(myCar.description)

    // 4 wheels, 0.0 mph

  • 8/12/2019 Intro Swift

    246/343

    Overriding a Property

    let myCar = Car()

    println(myCar.description)

    // 4 wheels, 0.0 mph

    myCar.speed = 35.0

  • 8/12/2019 Intro Swift

    247/343

    y p

    Overriding a Property

    let myCar = Car()

    println(myCar.description)

    // 4 wheels, 0.0 mph

    myCar.speed = 35.0

  • 8/12/2019 Intro Swift

    248/343

    println(myCar.description)

    // 4 wheels, 35.0 mph

    Property Observers

  • 8/12/2019 Intro Swift

    249/343

    Property Observers

    class ParentsCar: Car {

  • 8/12/2019 Intro Swift

    250/343

    }

    Property Observers

    class ParentsCar: Car {

    override var speed: Double {

  • 8/12/2019 Intro Swift

    251/343

    }

    }

    Property Observers

    class ParentsCar: Car {

    override var speed: Double {

    willSet {

    }

    didSet {

  • 8/12/2019 Intro Swift

    252/343

    }

    }

    }

    Property Observers

    class ParentsCar: Car {

    override var speed: Double {

    willSet {

    // newValue is available here

    }

    didSet {

  • 8/12/2019 Intro Swift

    253/343

    }

    }

    }

    Property Observers

    class ParentsCar: Car {

    override var speed: Double {

    willSet {

    }

    didSet {

  • 8/12/2019 Intro Swift

    254/343

    // oldValue is available here

    }

    }

    }

    Property Observers

    class ParentsCar: Car {

    override var speed: Double {

    willSet {

    }

  • 8/12/2019 Intro Swift

    255/343

    }

    }

    }

    Property Observers

    class ParentsCar: Car {

    override var speed: Double {

    willSet {

    if newValue > 65.0 {

    }

    }

  • 8/12/2019 Intro Swift

    256/343

    }

    }

    }

    Property Observers

    class ParentsCar: Car {

    override var speed: Double {

    willSet {

    if newValue > 65.0 {

    println("Careful now.")

    }

    }

  • 8/12/2019 Intro Swift

    257/343

    }

    }

    }

    Methods

  • 8/12/2019 Intro Swift

    258/343

    Methods

    class Counter {

    var count = 0

    }

  • 8/12/2019 Intro Swift

    259/343

    Methods

    class Counter {

    var count = 0

    }

  • 8/12/2019 Intro Swift

    260/343

    Methods

    class Counter {

    var count = 0

    func increment() {

    count++

    }

    }

  • 8/12/2019 Intro Swift

    261/343

    Methods

    class Counter {

    var count = 0

    func incrementBy(amount: Int) {

    count += amount

    }

    }

  • 8/12/2019 Intro Swift

    262/343

    Methods

    class Counter {

    var count= 0

    func incrementBy(amount: Int) {

    count+= amount

    }

    }

  • 8/12/2019 Intro Swift

    263/343

    Methods

    class Counter {

    var count= 0

    func incrementBy(amount: Int) {

    count += amount

    }

    func resetToCount(count: Int) {

    self.count = count

  • 8/12/2019 Intro Swift

    264/343

    self.count count

    }

    }

    Beyond Classes

  • 8/12/2019 Intro Swift

    265/343

    Tim IstedDeveloper Publications Engineer

    Structures in Swift

  • 8/12/2019 Intro Swift

    266/343

    Structures in Swift

    structPoint {

    var x, y: Double

    }

    struct Size{var width, height: Double

    }

    R {

  • 8/12/2019 Intro Swift

    267/343

    struct Rect{

    var origin: Pointvar size: Size

    }

  • 8/12/2019 Intro Swift

    268/343

    Structures in Swift

    var point = Point(x: 0.0, y: 0.0)

    var size = Size(width: 640.0, height: 480.0)

    var rect = Rect(origin: point, size: size)

  • 8/12/2019 Intro Swift

    269/343

    Structures in Swift

    struct Rect{

    var origin: Point

    var size: Size

    }

  • 8/12/2019 Intro Swift

    270/343

    Structures in Swift

    struct Rect {

    var origin: Point

    var size: Size

    var area: Double {return size.width * size.height

    }

    }

  • 8/12/2019 Intro Swift

    271/343

  • 8/12/2019 Intro Swift

    272/343

    Structures in Swift

    struct Rect {

    var origin: Point

    var size: Size

    var area: Double {return size.width * size.height

    }

    func isBiggerThanRect(other: Rect) -> Bool {

  • 8/12/2019 Intro Swift

    273/343

    func isBiggerThanRect(other: Rect) -> Bool {

    return self.area > other.area}

    }

    Structures and Classes

    structRect {

    var origin: Point

    var size: Size

    var area: Double {return size.width * size.height

    }

    }

  • 8/12/2019 Intro Swift

    274/343

    classWindow {var frame: Rect

    ...

    }

    var window

    Structure or Class?

    = Window(frame: frame)

  • 8/12/2019 Intro Swift

    275/343

    var window =

    Structure or Class?

    Window

    Window(frame: frame)

  • 8/12/2019 Intro Swift

    276/343

    var window

    Structure or Class?

    Window

    Window(frame: frame)

  • 8/12/2019 Intro Swift

    277/343

    var window

    setup(window)

    What if

    Window

    Window(frame: frame)

    Window

    Window(frame: frame)

  • 8/12/2019 Intro Swift

    278/343

    var window

    setup(window)

    What if

    Window

    Window(frame: frame)

    Window

    Window(frame: frame

  • 8/12/2019 Intro Swift

    279/343

    var window

    setup(window)

    What if

    Window

    Window(frame: frame)

  • 8/12/2019 Intro Swift

    280/343

    var window

    setup(window)

    func setup(window: Window) {

    // d

    Class Instances are Passed by Reference

    Window

    Window(frame: frame)

  • 8/12/2019 Intro Swift

    281/343

    // do some setup

    }

    What if

    Window

    var newFrame= window.frame

  • 8/12/2019 Intro Swift

    282/343

    What if

    var newFrame = window.frame

    newFrame.origin.x = 20.0

    Window

  • 8/12/2019 Intro Swift

    283/343

    What if

    var newFrame = window.frame

    newFrame.origin.x = 20.0

    Window

  • 8/12/2019 Intro Swift

    284/343

    Structures are Passed by Value

    var newFrame = window.frame

    newFrame.origin.x = 20.0

    Window

  • 8/12/2019 Intro Swift

    285/343

    letwindow

    Constants and Variables: Reference Types

    = Window(frame: frame)

  • 8/12/2019 Intro Swift

    286/343

    letwindow =

    Constants and Variables: Reference Types

    Window

    Window(frame: frame)

  • 8/12/2019 Intro Swift

    287/343

    letwindow

    Constants and Variables: Reference Types

    Window

    Window(frame: frame)

  • 8/12/2019 Intro Swift

    288/343

    letwindow

    window.title = "Hello!"

    Constants and Variables: Reference Types

    Window

    Window(frame: frame)

    Window

    Window(frame: frame)

  • 8/12/2019 Intro Swift

    289/343

    letwindow

    window.title = "Hello!"

    Constants and Variables: Reference Types

    Window

    Window(frame: frame)

    Hello!

    Window(frame: frame)

  • 8/12/2019 Intro Swift

    290/343

    letwindow

    window.title = "Hello!"

    Constants and Variables: Reference Types

    Window

    Window(frame: frame)

    Hello!

    Window(frame: frame)

  • 8/12/2019 Intro Swift

    291/343

    window = Window(frame: frame)

    letwindow

    window.title = "Hello!"

    Constants and Variables: Reference Types

    Window

    Window(frame: frame)

    Hello!

    Window(frame: frame)

  • 8/12/2019 Intro Swift

    292/343

    window = Window(frame: frame)// error: Cannot mutate a constant!

    Constants and Variables: Value Types

    varpoint1 = Point(x: 0.0, y: 0.0)

  • 8/12/2019 Intro Swift

    293/343

    x: 0.0

    y: 0.0

    Constants and Variables: Value Types

    varpoint1 = Point(x: 0.0, y: 0.0)

  • 8/12/2019 Intro Swift

    294/343

    x: 0.0

    y: 0.0

    Constants and Variables: Value Types

    varpoint1 = Point(x: 0.0, y: 0.0)

    point1.x = 5

  • 8/12/2019 Intro Swift

    295/343

    x: 5.0

    y: 0.0

    Constants and Variables: Value Types

    varpoint1 = Point(x: 0.0, y: 0.0)

    point1.x = 5

  • 8/12/2019 Intro Swift

    296/343

    x: 0.0

    y: 0.0

    x: 5.0

    y: 0.0

    Constants and Variables: Value Types

    varpoint1 = Point(x: 0.0, y: 0.0)

    point1.x = 5

    letpoint2 = Point(x: 0.0, y: 0.0)

  • 8/12/2019 Intro Swift

    297/343

    x: 0.0

    y: 0.0

    x: 5.0

    y: 0.0

    Constants and Variables: Value Types

    varpoint1 = Point(x: 0.0, y: 0.0)

    point1.x = 5

    letpoint2 = Point(x: 0.0, y: 0.0)

  • 8/12/2019 Intro Swift

    298/343

    point2.x = 5

    x: 0.0

    y: 0.0

    x: 5.0

    y: 0.0

    Constants and Variables: Value Types

    varpoint1 = Point(x: 0.0, y: 0.0)

    point1.x = 5

    letpoint2 = Point(x: 0.0, y: 0.0)

  • 8/12/2019 Intro Swift

    299/343

    point2.x = 5

    // error: Cannot mutate a constant!

    Mutating a Structure

    struct Point {

    var x, y: Double

    }

  • 8/12/2019 Intro Swift

    300/343

  • 8/12/2019 Intro Swift

    301/343

    Mutating a Structure

    struct Point {

    var x, y: Double

    mutating func moveToTheRightBy(dx: Double) {

    x += dx

    }

    }

  • 8/12/2019 Intro Swift

    302/343

    Mutating a Structure

    struct Point {

    var x, y: Double

    mutatingfunc moveToTheRightBy(dx: Double) {

    x += dx

    }

    }

    letpoint = Point(x: 0.0, y: 0.0)

    point.moveToTheRightBy(200.0)

  • 8/12/2019 Intro Swift

    303/343

    p g y

    // Error: Cannot mutate a constant!

    Enumerations: Raw Values

    enum Planet: Int{

    case Mercury = 1, Venus, Earth, Mars, Jupiter, Satur

    Uranus, Neptune

    }

  • 8/12/2019 Intro Swift

    304/343

    Enumerations: Raw Values

    enum Planet: Int{

    case Mercury = 1, Venus, Earth, Mars, Jupiter, Satur

    Uranus, Neptune

    }

    let earthNumber = Planet.Earth.toRaw()

    // earthNumber is 3

  • 8/12/2019 Intro Swift

    305/343

    Enumerations: Raw Values

    enum ControlCharacter: Character{

    case Tab= "\t"

    case Linefeed= "\n"

    case CarriageReturn= "\r"}

  • 8/12/2019 Intro Swift

    306/343

    Enumerations

    enum CompassPoint{

    case North, South, East, West

    }

  • 8/12/2019 Intro Swift

    307/343

    Enumerations

    enum CompassPoint {

    case North, South, East, West

    }

    var directionToHead = CompassPoint.West

    // directionToHead is inferred to be a CompassPoint

  • 8/12/2019 Intro Swift

    308/343

    Enumerations

    enum CompassPoint {

    case North, South, East, West

    }

    var directionToHead = CompassPoint.West

    // directionToHead is inferred to be a CompassPoint

    directionToHead = .East

  • 8/12/2019 Intro Swift

    309/343

    Enumerations

    enum CompassPoint {

    case North, South, East, West

    }

    var directionToHead = CompassPoint.West

    // directionToHead is inferred to be a CompassPoint

    directionToHead = .East

  • 8/12/2019 Intro Swift

    310/343

    let label = UILabel()label.textAlignment = .Right

    Enumerations: Associated Values

    enum TrainStatus {

    case OnTime

    case Delayed(Int)

    }

  • 8/12/2019 Intro Swift

    311/343

    Enumerations: Associated Values

    enum TrainStatus {

    case OnTime

    case Delayed(Int)

    }

  • 8/12/2019 Intro Swift

    312/343

    Enumerations: Associated Values

    enum TrainStatus {

    case OnTime

    case Delayed(Int)

    }

    var status = TrainStatus.OnTime

    // status is inferred to be a TrainStatus

  • 8/12/2019 Intro Swift

    313/343

    Enumerations: Associated Values

    enum TrainStatus {

    case OnTime

    case Delayed(Int)

    }

    var status = TrainStatus.OnTime

    // status is inferred to be a TrainStatus

    t t D l d(42)

  • 8/12/2019 Intro Swift

    314/343

    status = .Delayed(42)

    Enumerations

    enum TrainStatus {

    case OnTime, Delayed(Int)

  • 8/12/2019 Intro Swift

    315/343

    }

    Enumerations: Initializers

    enum TrainStatus {

    case OnTime, Delayed(Int)

    init() {

    self = OnTime

    }

  • 8/12/2019 Intro Swift

    316/343

    }

    Enumerations: Properties

    enum TrainStatus {

    case OnTime, Delayed(Int)

    init() {

    self = OnTime

    }

    var description: String {

    switch self {

    case OnTime:

    return "on time"

    case Delayed(let minutes):

    return "delayed by \(minutes) minute(s)"

    }

    }

  • 8/12/2019 Intro Swift

    317/343

    }

    }

    Enumerations

    var status = TrainStatus()

  • 8/12/2019 Intro Swift

    318/343

    Enumerations

    var status = TrainStatus()

    println("The train is \(status.description)")

    // The train is on time

  • 8/12/2019 Intro Swift

    319/343

    Enumerations

    var status = TrainStatus()

    println("The train is \(status.description)")

    // The train is on time

    status = .Delayed(42)

  • 8/12/2019 Intro Swift

    320/343

    Enumerations

    var status = TrainStatus()

    println("The train is \(status.description)")

    // The train is on time

    status = .Delayed(42)

    println("The train is now \(status.description)")

    // The train is now delayed by 42 minute(s)

  • 8/12/2019 Intro Swift

    321/343

    // The train is now delayed by 42 minute(s)

    Nested Types

    class Train {

    enum Status{

    case OnTime, Delayed(Int)

    init() {self = OnTime

    }

    var description: String { ... }

    }

    var status = Status()}

  • 8/12/2019 Intro Swift

    322/343

    var status = Status()}

    Extensions

  • 8/12/2019 Intro Swift

    323/343

    Extensions

    extensionSize {mutating func increaseByFactor(factor: Int) {

    width *= factor

    height *= factor

    }

    }

  • 8/12/2019 Intro Swift

    324/343

    Extensions

    extension CGSize{mutating func increaseByFactor(factor: Int) {

    width *= factor

    height *= factor

    }

    }

  • 8/12/2019 Intro Swift

    325/343

    Extensions

    extension Int{

    }

  • 8/12/2019 Intro Swift

    326/343

    Extensions

    extension Int {func repetitions(task: () -> ()) {

    for i in 0..self {

    task()

    }

    }}

  • 8/12/2019 Intro Swift

    327/343

    Extensions

    extension Int {func repetitions(task: () -> ()) {

    for i in 0..self {

    task()

    }

    }}

    500.repetitions({

    println("Hello!")

    })

  • 8/12/2019 Intro Swift

    328/343

    })

    Extensions

    extension Int {func repetitions(task: () -> ()) {

    for i in 0..self {

    task()

    }

    }}

    500.repetitions {

    println("Hello!")

    }

  • 8/12/2019 Intro Swift

    329/343

    }

    A Non-Generic Stack Structure

    struct IntStack{var elements = Int[]()

    mutating func push(element: Int) {

    elements.append(element)

    }

    mutating func pop() -> Int {

    return elements.removeLast()

    }

    }

  • 8/12/2019 Intro Swift

    330/343

    }

    A Non-Generic Stack Structure

    struct IntStack {var elements = Int[]()

    mutating func push(element: Int) {

    elements.append(element)

    }

    mutating func pop() -> Int{

    return elements.removeLast()

    }

    }

  • 8/12/2019 Intro Swift

    331/343

    A Generic Stack Structure

    struct Stack{var elements = T[]()

    mutating func push(element: T) {

    elements.append(element)

    }

    mutating func pop() -> T {

    return elements.removeLast()

    }

    }

  • 8/12/2019 Intro Swift

    332/343

    A Generic Stack Structure

    struct Stack{var elements = T[]()

    mutating func push(element: T) {

    elements.append(element)

    }

    mutating func pop() -> T {

    return elements.removeLast()

    }

    }

  • 8/12/2019 Intro Swift

    333/343

    A Generic Stack Structure

    struct Stack {...

    }

    var intStack = Stack()

    intStack.push(50)let lastIn = intStack.pop()

  • 8/12/2019 Intro Swift

    334/343

    A Generic Stack Structure

    struct Stack {...

    }

    var intStack = Stack()

    intStack.push(50)let lastIn = intStack.pop()

    var stringStack = Stack()

    stringStack.push("Hello")

    println(stringStack.pop())

  • 8/12/2019 Intro Swift

    335/343

    A Generic Stack Structure

    struct Stack {...

    }

    var intStack = Stack()

    intStack.push(50)let lastIn = intStack.pop()

    var stringStack = Stack()

    stringStack.push("Hello")

    println(stringStack.pop())

  • 8/12/2019 Intro Swift

    336/343

    Resources

  • 8/12/2019 Intro Swift

    337/343

    Resources

  • 8/12/2019 Intro Swift

    338/343

    Resources

  • 8/12/2019 Intro Swift

    339/343

    Resources

  • 8/12/2019 Intro Swift

    340/343

    Resources

  • 8/12/2019 Intro Swift

    341/343

    More Information

    Dave DeLongDeveloper Tools Evangelist

    [email protected]

    Documentation

    The Swift Programming LanguageUsing Swift with Cocoa and Objective-C

    http://developer.apple.com

    Apple Developer Forumshttp://devforums.apple.com

    http://devforums.apple.com/http://devforums.apple.com/
  • 8/12/2019 Intro Swift

    342/343

  • 8/12/2019 Intro Swift

    343/343