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

Swift Intro

Florian
July 08, 2014

Swift Intro

A short introduction and tidbits for the new programming language from Apple called Swift. I talked about this at our monthly mobile development user group meet up in Stuttgart (http://mobilemaultaschen.de)

Florian

July 08, 2014
Tweet

More Decks by Florian

Other Decks in Technology

Transcript

  1. Swift

    View Slide

  2. @florianbuerger

    View Slide

  3. “Writing code is
    interactive
    and fun”

    View Slide

  4. “Swift is ready for
    your next iOS and
    OS X project”

    View Slide

  5. Variablen

    View Slide

  6. var // mutable
    let // constant

    View Slide

  7. var // mutable
    let // constant

    View Slide

  8. Typableitung

    View Slide

  9. let implicitDouble = 70.0
    let explicitDouble:Double = 70

    View Slide

  10. Keine
    (implizite)
    Konvertierung

    View Slide

  11. let first = "First "
    let nine = 9
    let patch = first + nine

    View Slide

  12. let first = "First "
    let nine = 9
    let patch = first + nine
    'String' is not convertible to 'UInt8'

    View Slide

  13. let first = "First "
    let nine = 9
    let patch = first + String(nine)
    "First 9"

    View Slide

  14. Funktionen

    View Slide

  15. func sayHello(personName: String) -> String
    {
    let greeting = "Hello, " + personName
    return greeting
    }

    View Slide

  16. func deleteServer(server: Server) -> (Bool, NSError?)
    { }

    View Slide

  17. Optionals

    View Slide

  18. var name :String?

    View Slide

  19. R.I.P
    if (!object) { }

    View Slide

  20. R.I.P
    if (!object) { }
    Bedingung muss Bool zurückliefern

    View Slide

  21. var optionalName: String? =
    "John Appleseed"
    !
    var greeting = "Hello!"
    if let name = optionalName {
    greeting = "Hello, \
    (name)"
    }

    View Slide

  22. var optionalName: String? =
    "John Appleseed"
    !
    var greeting = "Hello!"
    if let name = optionalName {
    greeting = "Hello, \
    (name)"
    }

    View Slide

  23. var optionalName: String? =
    "John Appleseed"
    !
    var greeting = "Hello!"
    if let name = optionalName {
    greeting = "Hello, \
    (name)"
    }

    View Slide

  24. class Controller {
    var title: String
    }
    ! Class 'Controller' has no initializers

    View Slide

  25. class Controller {
    var title: String?
    }
    'String?' does not have a
    member named 'isEmpty'

    View Slide

  26. class Controller {
    var title: String!
    }

    View Slide

  27. var server: Server!
    Implicitly
    Unwrapped
    Optional Type

    View Slide

  28. var server: Server!

    View Slide

  29. Collections

    View Slide

  30. [Int]
    [1, 2, 3, 4]

    View Slide

  31. [key:object]
    ["key":"value"]

    View Slide

  32. var ints = [1,2]
    ints += 3
    !
    // [1,2,3]

    View Slide

  33. var servers =
    [Server]()
    (...)
    let aServer = (...)
    servers += aServer

    View Slide

  34. class Server: NSObject,
    Equatable, NSCoding {
    var gooRequests = 0
    var badRequests = 0
    var alias: NSString
    var url: NSURL
    (...)
    }

    View Slide

  35. var servers =
    [Server]()
    (...)
    let aServer = (...)
    servers += aServer

    View Slide

  36. func == (lhs: Server,
    rhs: Server) -> Bool
    {
    return
    lhs.alias == rhs.alias
    && lhs.url == rhs.url
    }

    View Slide

  37. func == (lhs: Server,
    rhs: Server) -> Bool
    {
    return
    lhs.alias == rhs.alias
    && lhs.url == rhs.url
    }

    View Slide

  38. func == (lhs: Server,
    rhs: Server) -> Bool
    {
    return
    lhs.alias == rhs.alias
    && lhs.url == rhs.url
    }

    View Slide

  39. protocol Equatable {
    func ==(lhs: Self, rhs: Self) -> Bool
    }

    View Slide

  40. protocol Equatable {
    func ==(lhs: Self, rhs: Self) -> Bool
    }

    View Slide

  41. protocol Comparable : Equatable {
    func <=(lhs: Self, rhs: Self) -> Bool
    func >=(lhs: Self, rhs: Self) -> Bool
    func >(lhs: Self, rhs: Self) -> Bool
    }

    View Slide

  42. protocol Comparable : Equatable {
    func <=(lhs: Self, rhs: Self) -> Bool
    func >=(lhs: Self, rhs: Self) -> Bool
    func >(lhs: Self, rhs: Self) -> Bool
    }

    View Slide

  43. Closures

    View Slide

  44. fuckingclosuresyntax.com

    View Slide

  45. // Variable
    var closureName:
    (parameterTypes) -> (returnType)

    View Slide

  46. // Typealias
    typealias closureType =
    (parameterTypes) -> (returnType)

    View Slide

  47. // Argument
    func({(parameterTypes) ->
    (returnType) in statements})

    View Slide

  48. // Parameter
    array.sort({ (item1: Int, item2:
    Int) -> Bool
    in
    return item1 < item2
    })

    View Slide

  49. // Parameter
    array.sort({ (item1: Int, item2:
    Int) -> Bool
    in
    return item1 < item2
    })

    View Slide

  50. // Parameter
    array.sort({ (item1: Int, item2:
    Int) -> Bool
    in
    return item1 < item2
    })

    View Slide

  51. // Parameter
    array.sort({ (item1: Int, item2:
    Int) -> Bool
    in
    return item1 < item2
    })

    View Slide

  52. // in kurz
    array.sort { $0 < $1 }

    View Slide

  53. Singleton

    View Slide

  54. let _SingletonASharedInstance =
    SingletonA()
    !
    class SingletonA {
    !
    class var sharedInstance : SingletonA {
    return _SingletonASharedInstance
    }
    !
    }

    View Slide

  55. Konstante
    public

    View Slide

  56. class Manager {
    class var sharedInstance :Manager {
    !
    struct Static {
    static let instance
    :Manager = ServerManager()
    }
    return Static.instance
    }
    }
    https://github.com/hpique/SwiftSingleton

    View Slide

  57. structs support
    static
    constants

    View Slide

  58. let _SingletonASharedInstance =
    SingletonA()
    !
    class Manager {
    !
    class var sharedInstance :Manager {
    return _SingletonASharedInstance
    }
    !
    } Private Konstanten (hoffentlich) in
    nächster Version

    View Slide

  59. Properties

    View Slide

  60. class Controller {
    var title: String = "aTitle" {
    !
    willSet(newTitle) {
    println("About to set
    title to \(newTitle)")
    }
    !
    }
    }

    View Slide

  61. class Controller {
    (...)
    didSet {
    println("Changed
    title \(oldValue) to \(title)")
    }
    (...)

    View Slide

  62. !
    let controller = Controller()
    controller.title = "Hello"
    !
    // Console output
    About to set title to Hello
    Changed title aTitle to Hello

    View Slide

  63. “The Swift Programming
    Language”
    !
    https://itunes.apple.com/de/book/swift-
    programming-language/id881256329?
    l=en&mt=11

    View Slide

  64. “Using Swift with Cocoa
    and Objective-C”
    !
    https://itunes.apple.com/de/book/using-
    swift-cocoa-objective/id888894773?l=en&mt=11

    View Slide

  65. Follow the Swift
    engineers
    (e.g. Chris Lattner)
    !
    https://twitter.com/olebegemann/status/
    482274302805700608

    View Slide