Swift Album ver.4.1

6a55a7fb19c1fa78e86ddca7a69db088?s=47 Yuka Ezura
December 18, 2017

Swift Album ver.4.1

6a55a7fb19c1fa78e86ddca7a69db088?s=128

Yuka Ezura

December 18, 2017
Tweet

Transcript

  1. 2.
  2. 5.

    ezura • iOS engineer @ LINE • Advent calendar 2017

    • Ͳ͏ͯͦ͠ͷػೳ/࢓༷͸Swiftʹͳ͍ͷʁ • Swift4.1+ @eduraaa
  3. 6.

    ezura • iOS engineer @ LINE • Advent calendar 2017

    • Ͳ͏ͯͦ͠ͷػೳ/࢓༷͸Swiftʹͳ͍ͷʁ • Swift4.1+ @eduraaa
  4. 10.

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

    v3: String } // CustomType instance == CustomType instance customInstance1 == customInstance2
  5. 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
  6. 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 } } ໌ࣔతʹ࣮૷͢Δඞཁ͕͋ͬͨ
  7. 13.
  8. 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 } }
  9. 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 } }
  10. 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` ʹద߹
  11. 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 } } }
  12. 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` ʹద߹
  13. 20.

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

    T: P {} ͱ͋Δ৚݅Λຬ͍ͨͯ͠Δ৔߹͚ͩ
 ܕʹ Protocol ͕ద߹͞ΕΔ
  14. 21.

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

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

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

    Array<Element>, rhs: Array<Element>) -> Bool {
 … } } ॻ͚ͳ͍ʂ
  17. 27.

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

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

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

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