Swift Intro

896e816a54b484b5233ed5f9ea5278a5?s=47 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)

896e816a54b484b5233ed5f9ea5278a5?s=128

Florian

July 08, 2014
Tweet

Transcript

  1. Swift

  2. @florianbuerger

  3. “Writing code is interactive and fun”

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

    project”
  5. Variablen

  6. var // mutable let // constant

  7. var // mutable let // constant

  8. Typableitung

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

  10. Keine (implizite) Konvertierung

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

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

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

    patch = first + String(nine) "First 9"
  14. Funktionen

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

    " + personName return greeting }
  16. func deleteServer(server: Server) -> (Bool, NSError?) { }

  17. Optionals

  18. var name :String?

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

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

  21. var optionalName: String? = "John Appleseed" ! var greeting =

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

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

    "Hello!" if let name = optionalName { greeting = "Hello, \ (name)" }
  24. class Controller { var title: String } ! Class 'Controller'

    has no initializers
  25. class Controller { var title: String? } 'String?' does not

    have a member named 'isEmpty'
  26. class Controller { var title: String! }

  27. var server: Server! Implicitly Unwrapped Optional Type

  28. var server: Server!

  29. Collections

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

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

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

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

    += aServer
  34. class Server: NSObject, Equatable, NSCoding { var gooRequests = 0

    var badRequests = 0 var alias: NSString var url: NSURL (...) }
  35. var servers = [Server]() (...) let aServer = (...) servers

    += aServer
  36. func == (lhs: Server, rhs: Server) -> Bool { return

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

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

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

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

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

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

    -> Bool func >=(lhs: Self, rhs: Self) -> Bool func >(lhs: Self, rhs: Self) -> Bool }
  43. Closures

  44. fuckingclosuresyntax.com

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

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

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

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

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

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

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

    return item1 < item2 })
  52. // in kurz array.sort { $0 < $1 }

  53. Singleton

  54. let _SingletonASharedInstance = SingletonA() ! class SingletonA { ! class

    var sharedInstance : SingletonA { return _SingletonASharedInstance } ! }
  55. Konstante public

  56. class Manager { class var sharedInstance :Manager { ! struct

    Static { static let instance :Manager = ServerManager() } return Static.instance } } https://github.com/hpique/SwiftSingleton
  57. structs support static constants

  58. let _SingletonASharedInstance = SingletonA() ! class Manager { ! class

    var sharedInstance :Manager { return _SingletonASharedInstance } ! } Private Konstanten (hoffentlich) in nächster Version
  59. Properties

  60. class Controller { var title: String = "aTitle" { !

    willSet(newTitle) { println("About to set title to \(newTitle)") } ! } }
  61. class Controller { (...) didSet { println("Changed title \(oldValue) to

    \(title)") } (...)
  62. ! let controller = Controller() controller.title = "Hello" ! //

    Console output About to set title to Hello Changed title aTitle to Hello
  63. “The Swift Programming Language” ! https://itunes.apple.com/de/book/swift- programming-language/id881256329? l=en&mt=11

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

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