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

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. ezura • iOS engineer @ LINE • Advent calendar 2017

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

    • Ͳ͏ͯͦ͠ͷػೳ/࢓༷͸Swiftʹͳ͍ͷʁ • Swift4.1+ @eduraaa
  3. struct CustomType { let v1: String let v2: String let

    v3: String } // CustomType instance == CustomType instance customInstance1 == customInstance2
  4. 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
  5. 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 } } ໌ࣔతʹ࣮૷͢Δඞཁ͕͋ͬͨ
  6. 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. 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 } }
  8. 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` ʹద߹
  9. 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 } } }
  10. 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` ʹద߹
  11. protocol P {} struct S<T> {} extension S: P where

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

    Array<Element>, rhs: Array<Element>) -> Bool {
 … } } `Element` ͕ `Equatable` ͷͱ͖
 ࣗ਎͕ `Equatable` ʹద߹
  13. extension Array: Equatable where Element: Equatable { static func ==(lhs:

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

    [[1, 2, 3]] `==` ͰൺֱͰ͖Δ͚ΕͲ
 `Array` ࣗମ͸ `Equatable` Ͱ͸ͳ͍
  15. extension Array: Equatable where Element: Equatable { static func ==(lhs:

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