$30 off During Our Annual Pro Sale. View Details »

Swift Album ver.4.1

Yuka Ezura
December 18, 2017

Swift Album ver.4.1

Yuka Ezura

December 18, 2017
Tweet

More Decks by Yuka Ezura

Other Decks in Programming

Transcript

  1. Swift Album
 ver.4.1

  2. Swift

  3. apple/swift

  4. What’s happen in Swift4.1??

  5. ezura • iOS engineer @ LINE • Advent calendar 2017

    • Ͳ͏ͯͦ͠ͷػೳ/࢓༷͸Swiftʹͳ͍ͷʁ • Swift4.1+ @eduraaa
  6. ezura • iOS engineer @ LINE • Advent calendar 2017

    • Ͳ͏ͯͦ͠ͷػೳ/࢓༷͸Swiftʹͳ͍ͷʁ • Swift4.1+ @eduraaa
  7. Topics • `Equatable`/`Hashable`ద߹ʹΑΔ`==`/`hashValue`ͷ҉໧త ࣮૷ • ಛఆͷ৚݅ԼͰͷϓϩτίϧద߹ • ࠶ىతͳϓϩτίϧ੍໿ • Unsafe[Mutable][Raw][Buffer]PointerͷAPIઃܭͷվળ

    • ϓϩτίϧ಺ͰͷΦʔφʔγοϓͷએݴΛ࡟আ • Unsafe[Mutable][Raw][Buffer]PointerͷAPIઃܭͷվળ
  8. Topics • `Equatable`/`Hashable`ద߹ʹΑΔ`==`/`hashValue`ͷ҉໧త ࣮૷ • ಛఆͷ৚݅ԼͰͷϓϩτίϧద߹ • ࠶ىతͳϓϩτίϧ੍໿ • Unsafe[Mutable][Raw][Buffer]PointerͷAPIઃܭͷվળ

    • ϓϩτίϧ಺ͰͷΦʔφʔγοϓͷએݴΛ࡟আ • Unsafe[Mutable][Raw][Buffer]PointerͷAPIઃܭͷվળ
  9. `==`/`hashValue`ͷ
 ҉໧త࣮૷

  10. struct CustomType { let v1: String let v2: String let

    v3: String } // CustomType instance == CustomType instance customInstance1 == customInstance2
  11. struct CustomType { let v1: String let v2: String let

    v3: String } // CustomType instance == CustomType instance customInstance1 == customInstance2 error: binary operator '==' cannot be applied to two 'CustomType' operands
  12. struct CustomType: Equatable { let v1: String let v2: String

    let v3: String static func ==(lhs: CustomType, rhs: CustomType) -> Bool { return lhs.v1 == rhs.v1 && lhs.v2 == rhs.v2 && lhs.v3 == rhs.v3 } } ໌ࣔతʹ࣮૷͢Δඞཁ͕͋ͬͨ
  13. ੒௕

  14. struct CustomType: Equatable { let v1: String let v2: String

    let v3: String static func ==(lhs: CustomType, rhs: CustomType) -> Bool { return lhs.v1 == rhs.v1 && lhs.v2 == rhs.v2 && lhs.v3 == rhs.v3 } }
  15. struct CustomType: Equatable { let v1: String let v2: String

    let v3: String static func ==(lhs: CustomType, rhs: CustomType) -> Bool { return lhs.v1 == rhs.v1 && lhs.v2 == rhs.v2 && lhs.v3 == rhs.v3 } }
  16. struct CustomType: Equatable { let v1: String let v2: String

    let v3: String static func ==(lhs: CustomType, rhs: CustomType) -> Bool { return lhs.v1 == rhs.v1 && lhs.v2 == rhs.v2 && lhs.v3 == rhs.v3 } } ৚݅: શͯ `Equatable` ʹద߹ ৚݅: એݴ෦Ͱ `Equatable` ʹద߹
  17. enum Token: Equatable { case string(String) case number(Int) case lparen

    case rparen case custom(CustomType) static func == (lhs: Token, rhs: Token) -> Bool { switch (lhs, rhs) { case (.string(let lhsString), .string(let rhsString)): return lhsString == rhsString case (.number(let lhsNumber), .number(let rhsNumber)): return lhsNumber == rhsNumber case (.custom(let lhsCustom), .custom(let rhsCustom)): return lhsCustom == rhsCustom
 case (.lparen, .lparen), (.rparen, .rparen): return true default: return false } } }
  18. enum Token: Equatable { case string(String) case number(Int) case lparen

    case rparen case custom(CustomType) static func == (lhs: Token, rhs: Token) -> Bool { switch (lhs, rhs) { case (.string(let lhsString), .string(let rhsString)): return lhsString == rhsString case (.number(let lhsNumber), .number(let rhsNumber)): return lhsNumber == rhsNumber case (.lparen, .lparen), (.rparen, .rparen): return true case (.custom(let lhsCustom), .custom(let rhsCustom)): return lhsCustom == rhsCustom default: return false } } } ৚݅: શͯ `Equatable` ʹద߹ ৚݅: એݴ෦Ͱ `Equatable` ʹద߹
  19. conditional conformance

  20. protocol P {} struct S<T> {} extension S: P where

    T: P {} ͱ͋Δ৚݅Λຬ͍ͨͯ͠Δ৔߹͚ͩ
 ܕʹ Protocol ͕ద߹͞ΕΔ
  21. extension Array: Equatable where Element: Equatable { static func ==(lhs:

    Array<Element>, rhs: Array<Element>) -> Bool {
 … } } `Element` ͕ `Equatable` ͷͱ͖
 ࣗ਎͕ `Equatable` ʹద߹
  22. ݱࡏ

  23. extension Array: Equatable where Element: Equatable { static func ==(lhs:

    Array<Element>, rhs: Array<Element>) -> Bool {
 … } } ॻ͚ͳ͍ʂ
  24. Array<Equtable ద߹ͷܕ> == Array<Equtable ద߹ͷܕ> 
 [1, 2, 3] ==

    [1, 2, 3]
  25. Array<Array<Equtable ద߹ͷܕ>> == Array<Array<Equtable ద߹ͷܕ>> 
 [[1, 2, 3]] ==

    [[1, 2, 3]]
  26. Array<Array<Equtable ద߹ͷܕ>> == Array<Array<Equtable ద߹ͷܕ>> 
 [[1, 2, 3]] ==

    [[1, 2, 3]]
  27. Array<Array<Equtable ద߹ͷܕ>> == Array<Array<Equtable ద߹ͷܕ>> 
 [[1, 2, 3]] ==

    [[1, 2, 3]] `==` ͰൺֱͰ͖Δ͚ΕͲ
 `Array` ࣗମ͸ `Equatable` Ͱ͸ͳ͍
  28. < Conditional conformances

  29. extension Array: Equatable where Element: Equatable { static func ==(lhs:

    Array<Element>, rhs: Array<Element>) -> Bool {
 … } } `Element` ͕ `Equatable` ͷͱ͖
 ࣗ਎͕ `Equatable` ʹద߹
  30. Array<Array<Equtable ద߹ͷܕ>> == Array<Array<Equtable ద߹ͷܕ>> 
 [[1, 2, 3]] ==

    [[1, 2, 3]] `Array` ࣗମ΋ `Equatable`
  31. ৄࡉ͸ͪ͜Β LINE Advent Calendar 2017
 Swift 4.1+
 https://engineering.linecorp.com/ja/blog/detail/227