Swift の世界観と
その秩序

Swift の世界観と
その秩序

6a55a7fb19c1fa78e86ddca7a69db088?s=128

Yuka Ezura

March 29, 2016
Tweet

Transcript

  1. Swift ͷੈք؍ͱ
 ͦͷடং 2016/3/29 ezura

  2. ಥવͰ͕͢

  3. Swift ͷͲ͕͜޷͖Ͱ͔͢ʁ

  4. ࢲ͸ɺۭؾͷΑ͏ʹ Protocol ͕ͨͪଘࡏ͠ɺ
 ಇ͖͔͚͍ͯΔ༷ࢠ͕޷͖Ͱ͢ɻ

  5. for element in [1, 2, 3] { /* do something

    */ } [1, 2, 3].forEach { _ in /* do something */ } 1 ... 3 if array.isEmpty { /* do something */ } SequenceType
 (CollectionType) ForwardIndexType BooleanType Comparable IntegerLiteral
 Convertible ArrayLiteral
 Convertible
  6. ͦΜͳɺۭؾͷΑ͏ʹࡏΓɺSwift ͷੈքΛ
 ࢧ͍͑ͯΔଘࡏ

  7. ඪ४ϥΠϒϥϦͷੈքΛ೷͍ͯΈ·͠ΐ͏

  8. Swift ͷੈք؍ͱ
 ͦͷடং ඪ४ϥΠϒϥϦ͕
 ૊Έ্͛Δ

  9. ୭ʁ • ៸໘༑߳ (ezura) • τϥΠϑΥʔτͰಇ͘ iOS ΤϯδχΞ • ݴޠͷઃܭ֓೦ͱ͔ߟ͑Δͷ͕޷͖

    • twitter: @eduraaa
  10. ࠓճɺ͓࿩͢͠Δ͜ͱ

  11. ඪ४ϥΠϒϥϦͷ৺Λཧղ͢Δ
 ಋೖฤ Swift ͷίΞ

  12. ໨ඪ
 ඪ४ϥΠϒϥϦΛಡΈղ͖΍͘͢ͳΔ͜ͱ

  13. ࠓճ͸ɺಛʹϓϩτίϧয఺Λ౰ͯͯΈ·͠ΐ͏

  14. • ࣗ෼ͰϓϩτίϧΛઃܭ͢Δͱ͖ͷࢀߟ • Ͱ͖Δ͜ͱͷ෯͕޿͕Δ ඪ४ϥΠϒϥϦͷϓϩτίϧΛཧղ͓ͯ͘͠ͱ…

  15. let elements = [2, 5, 1]
 elements.sort() // [1, 2,

    5] ྫ͑͹ɾɾɾ
  16. let elements: [CupSize] = [.Short, .Grande, .Short]
 
 // error:

    missing argument for parameter #1 in call elements.sort() ྫ͑͹ɾɾɾ
  17. enum CupSize: Int { case Short = 0, Tall, Grande,

    Venti } extension CupSize : Comparable {}
 // ུ let cupSizes: [CupSize] = [.Short, .Grande, .Short] cupSizes.sort() // [Short, Short, Grande] ഑ྻͷཁૉ͕ Comparable ʹ४ڌ͍ͯ͠Δͱɺ
 ͜ͷॻ͖ํͰ sort ͕࢖͑ΔΑ͏ʹͳΔ ྫ͑͹ɾɾɾ
  18. ɹͰ͸ɺຊ୊

  19. • ϓϩτίϧΛݟ͚ͭͯΈΔ • ݟ͚ͭͨϓϩτίϧΛ؍࡯ͯ͠ΈΔ • ඪ४ϥΠϒϥϦͷϓϩτίϧͨͪͱͷ஥ྑ͘ͳΓํ ൃදͷྲྀΕ

  20. • ϓϩτίϧΛݟ͚ͭͯΈΔ • ݟ͚ͭͨϓϩτίϧΛ؍࡯ͯ͠ΈΔ • ඪ४ϥΠϒϥϦͷϓϩτίϧͨͪͱͷ஥ྑ͘ͳΓํ

  21. for element in [1, 2, 3] { /* do something

    */ } [1, 2, 3].forEach { _ in /* do something */ } 1 ... 3 if array.isEmpty { /* do something */ } SequenceType
 (CollectionType) ForwardIndexType BooleanType Comparable IntegerLiteral
 Convertible ArrayLiteral
 Convertible
  22. for element in [1, 2, 3] { /* do something

    */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort()
  23. SequenceType for element in [1, 2, 3] { /* do

    something */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort() ࣭໰: Ͳ͏ͯ͠഑ྻ͸͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ Ұͭͷճ౴
 ഑ྻ͸ཁૉΛҰͭҰͭऔΓग़ͤΔ͔Β
  24. let array: Array = [1, 2, 3] struct Array<Element> :

    CollectionType
 SequenceType Λܧঝ for element in array { /* do something */ } array.forEach { _ in /* do something */ }
 
 array.sort(<)
  25. ඪ४ϥΠϒϥϦͷϓϩτίϧͷҰͭ
 SequenceType

  26. • ϓϩτίϧΛݟ͚ͭͯΈΔ • ݟ͚ͭͨϓϩτίϧΛ؍࡯ͯ͠ΈΔ • ඪ४ϥΠϒϥϦͷϓϩτίϧͨͪͱͷ஥ྑ͘ͳΓํ

  27. protocol SequenceType { associatedtype Generator : GeneratorType public func generate()

    -> Self.Generator
 public func map<T> … ུ public func forEach … ུ
 // ུ } extension SequenceType where Self.Generator.Element : Comparable { public func sort() -> [Self.Generator.Element] } SequenceType required
  28. extension MySequenceTypeStruct: SequenceType { func generate() -> AnyGenerator<Int> { var

    iteration = 0 return AnyGenerator { defer { iteration += 1 } guard iteration < 3 else { return nil } return iteration } } } let sequenceTypeStruct = MySequenceTypeStruct()
 
 for element in sequenceTypeStruct { print(element) } sequenceTypeStruct.sort() SequenceType
 ʹ४ڌ SequenceType
 ͷೳྗΛߦ࢖
  29. SequenceType
 
 ஋Λॱ൪ʹऔΕΔ
 ⁶
 Ұ࿈ͷ஋Λදݱ͢Δ΋ͷ Value Value Value … …

  30. protocol SequenceType { associatedtype Generator : GeneratorType public func generate()

    -> Self.Generator
 public func map<T> … ུ public func forEach … ུ } extension SequenceType where Self.Generator.Element : Comparable { public func sort() -> [Self.Generator.Element] } SequenceType
 Ұ࿈ͷ஋Λදݱ ஋ΛҰͭҰͭॱ൪ʹ
 औΓग़ͨ͢ΊͷػߏΛ࡞੒͢Δ
 ϝιου “Ұ࿈ͷ஋Λදݱ͢Δ” 
 ͜ͱʹΑͬͯͰ͖Δೳྗ
  31. for element in [1, 2, 3] { /* do something

    */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort() ࣭໰: Ͳ͏ͯ͠഑ྻ͸͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ
  32. ˛ ഑ྻ͔ͩΒ for … in ΍ forEach ͕࢖͑Δ
 ⭕️ SequenceType

    ʹ४ڌ͍ͯ͠Δ͔Β for … in ΍ 
 forEach ͕࢖͑Δ ⭕ ഑ྻ͕ʮҰ࿈ͷ஋Λදݱ͢Δʯ΋ͷ͔ͩΒɺ
 ͦͷੑ࣭͕࢖͑Δ ݫີʹ͸ Array ͸ SequenceType Λܧঝͨ͠
 CollectionType ʹ४ڌ͍ͯ͠·͢
  33. for element in [1, 2, 3] { /* do something

    */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort() SequenceType ࣭໰: Ͳ͏ͯ͠഑ྻ͸͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ
  34. for element in [1, 2, 3] { /* do something

    */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort()
 
 [1, 2, 3].count ❌ SequenceType
 ⭕️ CollectionType ࣭໰: Ͳ͏ͯ͠഑ྻ͸͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ
  35. ඪ४ϥΠϒϥϦͷϓϩτίϧ͸
 υΩϡϝϯτ্Ͱ֬ೝͰ͖Δ͚ͩͰ΋ɺ
 56 ݸ

  36. AbsoluteValuable AnyCollectionType AnyObject ArrayLiteralConvertible BidirectionalIndexType BitwiseOperationsType BooleanLiteralConvertible BooleanType CVarArgType CollectionType

    Comparable CustomDebugStringConverti ble CustomLeafReflectable CustomPlaygroundQuickLoo kable CustomReflectable CustomStringConvertible DictionaryLiteralConvertible Equatable ErrorType ExtendedGraphemeClusterL iteralConvertible FloatLiteralConvertible FloatingPointType ForwardIndexType GeneratorType Hashable Indexable IntegerArithmeticType IntegerLiteralConvertible IntegerType IntervalType LazyCollectionType LazySequenceType MirrorPathType MutableCollectionType MutableIndexable MutableSliceable NilLiteralConvertible OptionSetType OutputStreamType RandomAccessIndexType RangeReplaceableCollectio nType RawRepresentable ReverseIndexType SequenceType SetAlgebraType SignedIntegerType SignedNumberType Streamable Strideable StringInterpolationConvertibl e StringLiteralConvertible UnicodeCodecType UnicodeScalarLiteralConvert ible UnsignedIntegerType _ArrayBufferType _ArrayType _CVarArgAlignedType _CVarArgPassedAsDouble _CollectionWrapperType _DestructorSafeContainer _DisallowMixedSignArithmeti c _Incrementable _IntegerArithmeticType _IntegerType _MirrorType _NSArrayCoreType _NSCopyingType _NSDictionaryCoreType _NSDictionaryType _NSEnumeratorType _NSFastEnumerationType _NSSetCoreType _NSSetType _NSStringCoreType _ObjectiveCBridgeable _PointerType _RandomAccessAmbiguity _Reflectable _ReverseCollectionType _SequenceWrapperType _ShadowProtocol _SignedIntegerType _SinkType _StringElementType Swift Standard Library Reference ʹهࡌ͞Ε͍ͯͳ͍΋ͷؚΉ
  37. SequenceType ͸ණࢁͷҰ֯ʂ

  38. • ϓϩτίϧΛݟ͚ͭͯΈΔ • ݟ͚ͭͨϓϩτίϧΛ؍࡯ͯ͠ΈΔ • ඪ४ϥΠϒϥϦͷϓϩτίϧͨͪͱͷ஥ྑ͘ͳΓํ

  39. • ܧঝؔ܎Ͱ਌ͷ΋ͷ͔ΒݟΔ (্Ґͷ֓೦͔ΒݟΔ) • ίʔυ΍ϦϑΝϨϯεΛݟͯɺϓϩτίϧ͕ԿΛද ݱ͍ͨ͠ͷ͔ߟ͑ͯΈΔ
 Swift Standard Library Reference,

    SwiftDoc.org • ४ڌ͍ͯ͠ΔܕΛ࡞ͬͯΈΔ ݸਓతʹ࣮ફ͍ͯ͠Δ͜ͱ
  40. ϓϩτίϧ͕ԿΛදݱ͍ͨ͠ͷ͔ߟ͑ͯΈΔ

  41. Objective-C Ͱ΋ɺNSFastEnumeration ʹ४ڌ͢Ε͹ 
 for … in ͕࢖͑Δ @protocol NSFastEnumeration

    - (NSUInteger)countByEnumeratingWithState: (NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len; @end
  42. • for … in ͸ SequenceType ͷҰػೳ • ͦͷੑ࣭ɾ֓೦͔ͩΒͦ͜ͷೳྗΛϓϩτίϧଆͰ ఆٛ


    (ʮͦͷੑ࣭ɾ֓೦ͳΒ͜͏ಇ͘ΑͶʯ͕هड़͞Ε͍ͯΔ)
 forEach, sort, map, … Swift ͱ Objective-C ͰҟͳΔͱ͜Ζ
  43. • ಛఆͷλεΫɺػೳΛຬͨͨ͢Ίʹඞཁͳཁ݅Λ
 એݴ • ֦ு͢Δ͜ͱʹΑͬͯඞཁཁ݅ͷҰ෦Λຬͨͨ͠Γɺ ४ڌͨ͠ܕ͕࢖͑ΔػೳΛఏڙͰ͖Δ ࢀর: The Swift Programming

    Language (Swift 2.2) The Swift Programming Language ΑΓ Swift ʹ͓͚Δ Protocol ͱ͸
  44. • ಛఆͷλεΫɺػೳΛຬͨͨ͢Ίʹඞཁͳཁ݅Λ
 એݴ • ֦ு͢Δ͜ͱʹΑͬͯඞཁཁ݅ͷҰ෦Λຬͨͨ͠Γɺ ४ڌͨ͠ܕ͕࢖͑ΔػೳΛఏڙͰ͖Δ ࢀর: The Swift Programming

    Language (Swift 2.2) The Swift Programming Language ΑΓ ඪ४ϥΠϒϥϦͷϓϩτίϧͷઃܭ (஫: ࢲݟ) ͦͷੑ࣭ɾ ֓೦ͨΒ͠ΊΔ΋ͷͷએݴ ͦͷੑ࣭ɾ֓೦͔ͩΒͦ͜ͷೳྗ ͦͷੑ࣭ɾ֓೦ͨΒ͠ΊΔͨ Ίͷಛੑ
  45. ͦΜͳ؍఺ͰݟͯΔͱ…

  46. protocol SequenceType { associatedtype Generator : GeneratorType public func generate()

    -> Self.Generator
 public func map<T> … ུ public func forEach … ུ } extension SequenceType where Self.Generator.Element : Comparable { public func sort() -> [Self.Generator.Element] } SequenceType
 Ұ࿈ͷ஋Λදݱ ֓೦ɾੑ࣭Λදݱ͢ΔͨΊཁ݅
 ϝιουɾؔ਺ɾϓϩύςΟͳͲ ͦͷ֓೦ɾੑ࣭Ͱ͋Δ͕
 Ώ͑ʹ࣋ͭೳྗ ͦͷ֓೦ɾੑ࣭Ͱ͋Δ͕
 Ώ͑ʹ࣋ͭೳྗ
  47. Ұ͚ͭͩɺ࣮ફ

  48. • ܧঝؔ܎Ͱ਌ͷ΋ͷ͔ΒݟΔ (্Ґͷ֓೦͔ΒݟΔ) • ίʔυ΍ϦϑΝϨϯεΛݟͯɺϓϩτίϧ͕ԿΛද ݱ͍ͨ͠ͷ͔ߟ͑ͯΈΔ
 Swift Standard Library Reference,

    SwiftDoc.org • ४ڌ͍ͯ͠ΔܕΛ࡞ͬͯΈΔ ݸਓతʹ࣮ફ͍ͯ͠Δ͜ͱ
  49. for element in [1, 2, 3] { /* do something

    */ } [1, 2, 3].forEach { _ in /* do something */ }
 
 [1, 2, 3].sort()
 
 [1, 2, 3].count SequenceType ❌ SequenceType
 ⭕️ CollectionType
  50. CollectionType
 A multi-pass sequence with addressable positions. Կͱͳ͘Θ͔ΔΑ͏ͳɺΘ͔Βͳ͍Α͏ͳ…
 SequenceType ͱͲ͏ҧ͏ͷʁ

  51. ܧঝؔ܎ΛΈͯΈΔ SwiftDoc.org ͔ΒҾ༻

  52. SwiftDoc.org ͔ΒҾ༻ … … … … SequenceType
 Ұ࿈ͷ஋Λදݱ … …

    Indexable
 ఴࣈͰΞΫηεՄೳ 1 start end
  53. SequenceType
 Ұ࿈ͷ஋Λදݱ͢Δ΋ͷ Value Value Value … … + Indexable
 ࠷ॳͱ࠷ޙ͕ܾ·͍ͬͯΔ


    ఴࣈͰΞΫηεՄೳ
  54. SequenceType
 Ұ࿈ͷ஋Λදݱ͢Δ΋ͷ Value Value Value + Indexable
 ࠷ॳͱ࠷ޙ͕ܾ·͍ͬͯΔ
 ఴࣈͰΞΫηεՄೳ start

    end
  55. public protocol CollectionType : Indexable, SequenceType { associatedtype Generator :

    GeneratorType = 
 IndexingGenerator<Self> public func generate() -> Self.Generator // ... public var count: Self.Index.Distance { get } public var first: Self.Generator.Element? { get } } // ... ֓೦ɾੑ࣭Λදݱ ֓೦ɾੑ࣭Λදݱ ͦͷੑ࣭ɾ֓೦
 ͔ͩΒͦ͜ͷೳྗ ্Ґ֓೦
  56. CollectionType
 
 A multi-pass sequence with addressable positions. Value Value

    Value Value Value
  57. • ܧঝؔ܎Ͱ਌ͷ΋ͷ͔ΒݟΔ (্Ґͷ֓೦͔ΒݟΔ) • ίʔυ΍ϦϑΝϨϯεΛݟͯɺϓϩτίϧ͕ԿΛද ݱ͍ͨ͠ͷ͔ߟ͑ͯΈΔ
 Swift Standard Library Reference,

    SwiftDoc.org • ४ڌ͍ͯ͠ΔܕΛ࡞ͬͯΈΔ ݸਓతʹ࣮ફ͍ͯ͠Δ͜ͱ
  58. • ϓϩτίϧΛݟ͚ͭͯΈΔ • ݟ͚ͭͨϓϩτίϧΛ؍࡯ͯ͠ΈΔ • ඪ४ϥΠϒϥϦͷϓϩτίϧͨͪͱͷ஥ྑ͘ͳΓํ

  59. for element in [1, 2, 3] { /* do something

    */ } [1, 2, 3].forEach { _ in /* do something */ } 1 ... 3 if array.isEmpty { /* do something */ } SequenceType
 (CollectionType) ForwardIndexType BooleanType Comparable IntegerLiteral
 Convertible ArrayLiteral
 Convertible
  60. enum CupSize: Int { case Short = 0, Tall, Grande,

    Venti } extension CupSize : Comparable {}
 // ུ let cupSizes: [CupSize] = [.Short, .Grande, .Short] cupSizes.sort() // [Short, Short, Grande] let numbers = [2, 5, 1]
 numbers.sort() // [1, 2, 5] Comparable
  61. ·ͱΊ • ඪ४ϥΠϒϥϦͷϓϩτίϧͷ࡞Δடংͷ্Ͱ༷ʑ ͳॲཧ͕ಈ͍͍ͯΔ • ϓϩτίϧ͕ԿΛදݱ͍ͨ͠ͷ͔ɺಡΈղ͍ͯΈΔ ͱָ͍͠

  62. ϓϩτίϧɺδΣωϦοΫؔ਺͕ͨͪؔ܎͠߹͏༷ࢠ
 (͓౔࢈ʹ͠·͢ɻ༡ΜͰΈ͍ͯͩ͘͞ɻ) ೖΓ͖Βͳ͔ͬͨ΋ͷ
 https://github.com/ezura/SwiftPlayground/tree/ master/stdlib_sample.playground

  63. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠

  64. ࢀߟ • The Swift Programming Language (Swift 2.2)
 Language Guide

    / Protocols
 https://developer.apple.com/library/ios/documentation/Swift/ Conceptual/Swift_Programming_Language/Protocols.html#// apple_ref/doc/uid/TP40014097-CH25-ID267 • Swift Standard Library Reference
 The Swift Standard Library
 https://developer.apple.com/library/ios/documentation/ General/Reference/SwiftStandardLibraryReference/#protocols • SwiftDoc.org
 http://swiftdoc.org/v2.2/protocol