Share
Embed
Swift の世界観と その秩序
Swift 愛好会で発表した資料です。
http://eventdots.jp/event/582698?utm_source=event_582698&utm_medium=social&utm_campaign=feed&utm_content=fb100006405574207
Yuka Ezura
March 29, 2016
Transcript
64 slides
-
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 -
୭ʁ • ៸໘༑߳ (ezura) • τϥΠϑΥʔτͰಇ͘ iOS ΤϯδχΞ • ݴޠͷઃܭ֓೦ͱ͔ߟ͑Δͷ͕͖
• twitter: @eduraaa -
let elements: [CupSize] = [.Short, .Grande, .Short] // error:
missing argument for parameter #1 in call elements.sort() ྫ͑ɾɾɾ -
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 ͕͑ΔΑ͏ʹͳΔ ྫ͑ɾɾɾ -
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 -
for element in [1, 2, 3] { /* do something
*/ } [1, 2, 3].forEach { _ in /* do something */ } [1, 2, 3].sort() -
SequenceType for element in [1, 2, 3] { /* do
something */ } [1, 2, 3].forEach { _ in /* do something */ } [1, 2, 3].sort() ࣭: Ͳ͏ͯ͠ྻ͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ Ұͭͷճ ྻཁૉΛҰͭҰͭऔΓग़ͤΔ͔Β -
let array: Array = [1, 2, 3] struct Array<Element> :
CollectionType SequenceType Λܧঝ for element in array { /* do something */ } array.forEach { _ in /* do something */ } array.sort(<) -
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 -
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 ͷೳྗΛߦ -
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 Ұ࿈ͷΛදݱ ΛҰͭҰͭॱ൪ʹ औΓग़ͨ͢ΊͷػߏΛ࡞͢Δ ϝιου “Ұ࿈ͷΛදݱ͢Δ” ͜ͱʹΑͬͯͰ͖Δೳྗ -
for element in [1, 2, 3] { /* do something
*/ } [1, 2, 3].forEach { _ in /* do something */ } [1, 2, 3].sort() ࣭: Ͳ͏ͯ͠ྻ͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ -
˛ ྻ͔ͩΒ for … in forEach ͕͑Δ ⭕️ SequenceType
ʹ४ڌ͍ͯ͠Δ͔Β for … in forEach ͕͑Δ ⭕ ྻ͕ʮҰ࿈ͷΛදݱ͢Δʯͷ͔ͩΒɺ ͦͷੑ࣭͕͑Δ ݫີʹ Array SequenceType Λܧঝͨ͠ CollectionType ʹ४ڌ͍ͯ͠·͢ -
for element in [1, 2, 3] { /* do something
*/ } [1, 2, 3].forEach { _ in /* do something */ } [1, 2, 3].sort() SequenceType ࣭: Ͳ͏ͯ͠ྻ͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ -
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 ࣭: Ͳ͏ͯ͠ྻ͜ΕΒͷૢ࡞͕Ͱ͖Δͷʁ -
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 ʹهࡌ͞Ε͍ͯͳ͍ͷؚΉ -
• ܧঝؔͰͷͷ͔ΒݟΔ (্Ґͷ֓೦͔ΒݟΔ) • ίʔυϦϑΝϨϯεΛݟͯɺϓϩτίϧ͕ԿΛද ݱ͍ͨ͠ͷ͔ߟ͑ͯΈΔ Swift Standard Library Reference,
SwiftDoc.org • ४ڌ͍ͯ͠ΔܕΛ࡞ͬͯΈΔ ݸਓతʹ࣮ફ͍ͯ͠Δ͜ͱ -
Objective-C ͰɺNSFastEnumeration ʹ४ڌ͢Ε for … in ͕͑Δ @protocol NSFastEnumeration
- (NSUInteger)countByEnumeratingWithState: (NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len; @end -
• for … in SequenceType ͷҰػೳ • ͦͷੑ࣭ɾ֓೦͔ͩΒͦ͜ͷೳྗΛϓϩτίϧଆͰ ఆٛ
(ʮͦͷੑ࣭ɾ֓೦ͳΒ͜͏ಇ͘ΑͶʯ͕هड़͞Ε͍ͯΔ) forEach, sort, map, … Swift ͱ Objective-C ͰҟͳΔͱ͜Ζ -
• ಛఆͷλεΫɺػೳΛຬͨͨ͢Ίʹඞཁͳཁ݅Λ એݴ • ֦ு͢Δ͜ͱʹΑͬͯඞཁཁ݅ͷҰ෦Λຬͨͨ͠Γɺ ४ڌͨ͠ܕ͕͑ΔػೳΛఏڙͰ͖Δ ࢀর: The Swift Programming
Language (Swift 2.2) The Swift Programming Language ΑΓ Swift ʹ͓͚Δ Protocol ͱ -
• ಛఆͷλεΫɺػೳΛຬͨͨ͢Ίʹඞཁͳཁ݅Λ એݴ • ֦ு͢Δ͜ͱʹΑͬͯඞཁཁ݅ͷҰ෦Λຬͨͨ͠Γɺ ४ڌͨ͠ܕ͕͑ΔػೳΛఏڙͰ͖Δ ࢀর: The Swift Programming
Language (Swift 2.2) The Swift Programming Language ΑΓ ඪ४ϥΠϒϥϦͷϓϩτίϧͷઃܭ (: ࢲݟ) ͦͷੑ࣭ɾ ֓೦ͨΒ͠ΊΔͷͷએݴ ͦͷੑ࣭ɾ֓೦͔ͩΒͦ͜ͷೳྗ ͦͷੑ࣭ɾ֓೦ͨΒ͠ΊΔͨ Ίͷಛੑ -
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 Ұ࿈ͷΛදݱ ֓೦ɾੑ࣭Λදݱ͢ΔͨΊཁ݅ ϝιουɾؔɾϓϩύςΟͳͲ ͦͷ֓೦ɾੑ࣭Ͱ͋Δ͕ Ώ͑ʹ࣋ͭೳྗ ͦͷ֓೦ɾੑ࣭Ͱ͋Δ͕ Ώ͑ʹ࣋ͭೳྗ -
• ܧঝؔͰͷͷ͔ΒݟΔ (্Ґͷ֓೦͔ΒݟΔ) • ίʔυϦϑΝϨϯεΛݟͯɺϓϩτίϧ͕ԿΛද ݱ͍ͨ͠ͷ͔ߟ͑ͯΈΔ Swift Standard Library Reference,
SwiftDoc.org • ४ڌ͍ͯ͠ΔܕΛ࡞ͬͯΈΔ ݸਓతʹ࣮ફ͍ͯ͠Δ͜ͱ -
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 -
SwiftDoc.org ͔ΒҾ༻ … … … … SequenceType Ұ࿈ͷΛදݱ … …
Indexable ఴࣈͰΞΫηεՄೳ 1 start end -
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 } } // ... ֓೦ɾੑ࣭Λදݱ ֓೦ɾੑ࣭Λදݱ ͦͷੑ࣭ɾ֓೦ ͔ͩΒͦ͜ͷೳྗ ্Ґ֓೦ -
• ܧঝؔͰͷͷ͔ΒݟΔ (্Ґͷ֓೦͔ΒݟΔ) • ίʔυϦϑΝϨϯεΛݟͯɺϓϩτίϧ͕ԿΛද ݱ͍ͨ͠ͷ͔ߟ͑ͯΈΔ Swift Standard Library Reference,
SwiftDoc.org • ४ڌ͍ͯ͠ΔܕΛ࡞ͬͯΈΔ ݸਓతʹ࣮ફ͍ͯ͠Δ͜ͱ -
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 -
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 -
ࢀߟ • 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