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

Swift 1.2 The long-awaited language updates

Swift 1.2 The long-awaited language updates

Presented at Cocoa勉強会関西 #61

cockscomb

April 11, 2015
Tweet

More Decks by cockscomb

Other Decks in Programming

Transcript

  1. let condition = true let aNumber: Int? = 3 let

    anotherNumber: Int? = 7 if condition { if let a = aNumber { if let b = anotherNumber { if a < b { println(a + b) } } } } Swift 1.1
  2. let condition = true let aNumber: Int? = 3 let

    anotherNumber: Int? = 7 if condition, let a = aNumber, let b = anotherNumber where a < b { println(a + b) } Swift 1.2
  3. let hour = NSCalendar.currentCalendar() .component(.CalendarUnitHour, fromDate: NSDate()) var meridiem: String

    if hour < 12 { meridiem = "AM" } else { meridiem = "PM" } println(meridiem) Swift 1.1
  4. let hour = NSCalendar.currentCalendar() .component(.CalendarUnitHour, fromDate: NSDate()) let meridiem: String

    if hour < 12 { meridiem = "AM" } else { meridiem = "PM" } println(meridiem) Swift 1.2
  5. Set

  6. let abc: NSSet = NSSet(objects: "A", "B", "C") if NSSet(objects:

    "A").isSubsetOfSet(abc) { println(abc) } Swift 1.1
  7. class StaticPropertiesAndMethods { static func printDate() { println(date) } static

    let date = NSDate() } StaticPropertiesAndMethods.printDate() // => "2015-04-10 07:01:00 +0000" Swift 1.2
  8. zip

  9. let ordinals = ["first", "second", "third"] let values = [1,

    2, 3] var ordinalsDict: [String: Int] = [:] for (number, key) in enumerate(ordinals) { if number < values.count { ordinalsDict[key] = values[number] } } println(ordinalsDict) // => ["first": 1, "second": 2, "third": 3] Swift 1.1
  10. let ordinals = ["first", "second", "third"] let values = [1,

    2, 3] var ordinalsDict: [String: Int] = [:] for (key, value) in zip(ordinals, values) { ordinalsDict[key] = value } println(ordinalsDict) // => ["first": 1, "second": 2, "third": 3] Swift 1.2
  11. let any: AnyObject = 3 let number: Int = any

    as Int @objc protocol SomeProtocol { func something() -> String } class SomeClass: SomeProtocol { func something() -> String { return "Something awesome" } } let some: AnyObject = SomeClass() if some is SomeProtocol { println((some as SomeProtocol).something()) } Swift 1.1
  12. let any: AnyObject = 3 let number: Int = any

    as! Int protocol SomeProtocol { func something() -> String } class SomeClass: SomeProtocol { func something() -> String { return "Something awesome" } } let some: AnyObject = SomeClass() if some is SomeProtocol { println((some as! SomeProtocol).something()) } Swift 1.2
  13. class Prefix { let string: String let prefix: String? init(string:

    String, prefix: String? = nil) { self.string = string self.prefix = prefix } func perform() -> String { return prefix.map { $0 + self.string } ?? string } } println(Prefix(string: "String", prefix: "NS").perform()) // => "NSString" println(Prefix(string: "String").perform()) // => "String" Swift 1.1
  14. class Prefix { let string: String let prefix: String? init(string:

    String, prefix: String? = nil) { self.string = string self.prefix = prefix } func perform() -> String { return prefix.map { $0 + string } ?? string } } println(Prefix(string: "String", prefix: "NS").perform()) // => "NSString" println(Prefix(string: "String").perform()) // => "String" Swift 1.2
  15. enum Optional<T> : Reflectable, NilLiteralConvertible { case None case Some(T)

    func map<U>(f: @noescape (T) -> U) -> U? } Swift 1.2
  16. let mapped = ["A B C", "D E", "F"].map {

    $0.componentsSeparatedByString(" ") } println(mapped) // => [[A, B, C], [D, E], [F]] let flatMapped = ["A B C", "D E", "F"].flatMap { $0.componentsSeparatedByString(" ") } println(flatMapped) // => [A, B, C, D, E, F] Swift 1.2
  17. func toInt(value: String) -> Int? { return value.toInt() } let

    one: String? = "1" let mapOne = one.map { toInt($0) } println(mapOne) // => Optional(Optional(1)) let flatMapOne = one.flatMap { toInt($0) } println(flatMapOne) // => Optional(1) Swift 1.2
  18. let fonts = "/System/Library/Fonts" let files = NSFileManager() .enumeratorAtPath(fonts)! while

    let path: AnyObject = files.nextObject() { println(path) } Swift 1.1
  19. Breaking changes • as as? as! • Remove implicit conversions

    from NSString/ NSArray/NSDictionary to String/Array/ Dictionary • countElements → count
  20. Performances • Tools • Incremental build • Type checker •

    Swift • Improved many cases • Whole Module Optimization