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

Swiftroduction – An Introduction to Swift

Swiftroduction – An Introduction to Swift

Slides from the talk I gave at the Swift Warsaw #1 event.

http://swiftwarsaw.com

Łukasz Kuczborski

July 31, 2014
Tweet

More Decks by Łukasz Kuczborski

Other Decks in Programming

Transcript

  1. WHAT WILL BE COVERED CONSTANTS AND VARIABLES TYPE INFERENCE OPTIONALS

    TUPLES GENERICS CLOSURES SWITCH PLAYGROUNDS AND SWIFT REPL
  2. TYPE INFERENCE let x = 1 // inferred as Int

    let y = "Hello Swift Warsaw!" // inferred as String let z = 3.14 // inferred as Double let isItAwesome = true // inferred as Bool
  3. VARIABLES var x = 1 x = 2 x =

    "String" error: type 'Int' does not conform to protocol 'StringLiteralConvertible'
  4. Objective-C Person *person = ...; NSMutableString *description = [[NSMutableString alloc]

    init]; [description appendFormat:@"%@ is %i years old.", person.name, person.age]; if (person.employer) { [description appendFormat:@" They work for %@.", person.employer]; } else { [description appendString:@" They are unemployed."]; }
  5. Swift var description = "" description += "\(person.name) is \(person.age)

    years old." if person.employer { description += " They work for \(person.employer)." } else { description += " They are unemployed." }
  6. HOW ABOUT SOME EMOJI? ! let ! = "We" let

    " = "love" let # = "Swift" let $ = ! + " " + " + " " + # + "!" println($) // We love Swift! let cat: Character = "%" println("This is a cat -> \(cat)") for characters in "&&&&&" { println(character) }
  7. OPTIONALS ▸ Like Nullable in C# You use optionals in

    situations where a value may be absent — "The Swift Programming Language", Apple
  8. DECLARING AN OPTIONAL var errorCode: Int? // value is absent

    errorCode = 404 // value is set to 404
  9. OPTIONAL BINDING if let knownErrorCode = errorCode { println("Error code

    with number \(knownErrorCode) occurred!") } else { println("No error") }
  10. OPTIONAL BINDING WITH SHADOW VARIABLE if let errorCode = errorCode

    { println("Error code with number \(errorCode) occurred!") } else { println("No error") }
  11. DECOMPOSING TUPLES let (statusCode, statusMessage) = http404Error println("The status code

    is \(statusCode)") println("The status message is \(statusMessage)")
  12. ProTip: Name the elements of a tuple... let http200Status =

    (statusCode: 200, description: "OK") ...and then do this println("The status code is \(http200Status.statusCode)") println("The status message is \(http200Status.description)")
  13. GENERICS ▸ Like templates in C++ ▸ Swift is strict

    about types - You must declare a function to take parameters of certain type ▸ Sometimes you have a functionality that is the same for multiple different types
  14. struct IntStack { var items = [Int]() mutating func push(item:

    Int) { items.append(item) } mutating func pop() -> Int { return items.removeLast() } }
  15. struct Stack<T> { var items = [T]() mutating func push(item:

    T) { items.append(item) } mutating func pop() -> T { return items.removeLast() } }
  16. CLOSURES ▸ Like blocks in Objective-C ▸ Can capture and

    store reference to any constants and variables from the context in which they are defined ▸ Functions are actually special cases of closures ▸ Expressions - unnamed closures
  17. Objective-C if ([person.name isEqualToString:@"Matt Galloway"]) { NSLog(@"Author of an interesting

    Swift article"); } else if ([person.name isEqualToString:@"Ray Wenderlich"]) { NSLog(@"Has a great website"); } else if ([person.name isEqualToString:@"Tim Cook"]) { NSLog(@"CEO of Apple Inc."); } else { NSLog(@"Someone else); }
  18. Swift switch person.name { case "Matt Galloway": println("Author of an

    interesting Swift article") case "Ray Wenderlich": println("Has a great website") case "Tim Cook": println("CEO of Apple Inc.") default: println("Someone else") }
  19. switch i { case 0, 1, 2: println("Small") case 3...7:

    println("Medium") case 8..<10: println("Large") case _ where i % 2 == 0: println("Even") case _ where i % 2 == 1: println("Odd") default: break }
  20. WHAT IS A PLAYGROUND? ▸ Playground is an interactive environment

    for your Swift code ▸ Let's you try out some features/code snippets and get quick feedback without requiring you to compile and run a project ▸ Evaluates your code as you type it
  21. WHAT IS REPL? A read–eval–print loop (REPL), also known as

    an interactive toplevel or language shell, is a simple, interactive computer programming environment that takes single user inputs (i.e. single expressions), evaluates them, and returns the result to the user; a program written in a REPL environment is executed piecewise. — Wikipedia
  22. RESOURCES https://developer.apple.com/videos/wwdc/2014/ Session #402 - Introduction to Swift Session #403

    - Intermediate Swift Session #404 - Advanced Swift https://developer.apple.com/swift/resources/ http://www.raywenderlich.com