Swift/IOS Terms Lecture 3 CSE 438 WUSTL FALL 2016

  1. String interpolation
    A way in Swift to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. Each item that you insert into the string literal is wrapped in a pair of parentheses, prefixed by a backslash.
  2. Set
    A collection that stores distinct elements with no defined order

    var favoriteGenres: _?_<String> = [“Rock”, “Classical”, “Hip hop”]
  3. Closed Range Operator
    An operator (...) that lets you create a range of numbers that includes both the lower and upper values. used in for loops:

    for num in 1…5 {


       print
    (“(num) Nmes 4 is (num * 4)”)

    }
  4. Closure
    Self-contained blocks of functionality that can be passed around.

    let displayGreeting = {print(“Hello Class”)}


    let displayGreetng: () -> () = {print(“Hello Class”)}

    displayGreeting()
  5. Optionals
    Handle the absence of a value– There is a value and it equals x or there isn’t a value

    var numberOfLegs = [“ant”: 6, “snake”: 0, “cow” :4]

    let possibleNumLegs = numberOfLegs[“goat”] ???


    let possibleNumLegs: Int? = numberOfLegs[“goat”] //Value or nil


    if possibleNumLegs != nil {


       let legCount = possibleNumLegs! //Use ! to unwrap the optional

       print(“Goat has (legCount) legs)

    }


  6. Short cut for this optional would look like?

    var numberOfLegs = [“ant”: 6, “snake”: 0, “cow” :4]
    let possibleNumLegs = numberOfLegs[“goat”] ???

    let possibleNumLegs: Int? = numberOfLegs[“goat”] //Value or nil

    if possibleNumLegs != nil {  

       let legCount = possibleNumLegs! //Use ! to unwrap the optional  

       print(“Goat has (legCount) legs”)

    }
    let possibleNumLegs = numberOfLegs[“goat”]


    if
    let legCount = possibleNumLegs {

       print(Goat has (legCount) legs)

    }
  7. half-open range operator (a..<b)
    Defines a range that runs from a to b, but does not include b.

    It is said to be half-open because it contains its first value, but not its final value. As with the closed range operator, the value of a must not be greater than b. If the value of a is equal to b, then the resulting range will be empty.
  8. (T/F)Swift Strings cannot be indexed by integer values.
    TRUE


    Explanation: Different characters require different amounts of storage. Instead, you  traverse over each Unicode scalar from the start or end of the string.
  9. let greeting = "Guten Tag!"

    greeting[greeting.startIndex]

    What does this print?
    G
  10. let greeting = "Guten Tag!"

    greeting[greeting.endIndex.predecessor()]

    What does this code access?
    !
  11. let greeting = "Guten Tag!"

    greeting[greeting.startIndex.successor()]

    What character does this code access?
    u
  12. let index = greetng.startIndex.advancedBy(7)

    greeting[index]

    What character of the code does this access?
    a (you might think T but its startIndex + 7)
  13. Self-contained pieces of code to perform a task.
    Function
  14. In functions what are the differences between external and local parameter names
    • External Parameter labels arguments passed to a function call
    • Local Parameter Name used in implementation of function.


    func someFunction(1, secondParameterName: 2)
  15. (T/F) In functions, You may omit the external name for all arguments.
    TRUE

    Implementation:

    func someFunction(firstParameterName: Int, _ secondParameterName: Int)

    call:

    someFunction(1, 2)
  16. Write a function interface with a default parameter.
    func someFunction(parameterWithDefault: Int = 12)
  17. Variadic Parameters
    Allows for zero or more values for input parameter. Made available as an array.

    func arithmetiMean(numbers: Double...) -> Double
  18. In-Out Parameters
    Gives the ability to change the value of a parameter:

    definition:

    func swapTwoInts(inout a: Int, inout _ b: Int) {...}

    function call:

    swapTwoInts(&someInt, &anotherInt)
  19. (T/F)In swift the following line of code:

    var mathFunction: (Int, Int) -> Int = addTwoInts

    is legal?
    TRUE


    func addTwoInts(a: Int, _ b: Int) -> Int {return a + b}
  20. Write an implementation for a nested function:
    func chooseStepFunction(backwards: Bool) -> (Int) -> Int {

    func stepForward(input: Int) -> Int { return input + 1 }

    func stepBackward(input: Int) -> Int { return input - 1 }

    return backwards ? stepBackward : stepForward

    }
Author
geschw66
ID
322051
Card Set
Swift/IOS Terms Lecture 3 CSE 438 WUSTL FALL 2016
Description
Misc. terms from the Apple Developer Library site: //developer.apple.com/library/
Updated