Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Protocol in Swift

1 444 vues

Publié le

@横浜へなちょこ

Publié dans : Ingénierie
  • Soyez le premier à commenter

Protocol in Swift

  1. 1. Protocol in Swift
  2. 2. What's protocol?
  3. 3. A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.
  4. 4. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.
  5. 5. Examples
  6. 6. struct Int • Equatable • Comparable • IntegerArithmeticType, _IntegerArithmeticType • SignedIntegerType, _SignedIntegerType • IntegerType, _IntegerType
  7. 7. protocol Equatable { func ==(lhs: Self, rhs: Self) -> Bool }
  8. 8. protocol Comparable : Equatable { func <(lhs: Self, rhs: Self) -> Bool func <=(lhs: Self, rhs: Self) -> Bool func >=(lhs: Self, rhs: Self) -> Bool func >(lhs: Self, rhs: Self) -> Bool }
  9. 9. protocol IntegerArithmeticType : _IntegerArithmeticType, Comparable { func +(lhs: Self, rhs: Self) -> Self func -(lhs: Self, rhs: Self) -> Self func *(lhs: Self, rhs: Self) -> Self func /(lhs: Self, rhs: Self) -> Self func %(lhs: Self, rhs: Self) -> Self func toIntMax() -> IntMax }
  10. 10. struct Array<T> • CollectionType • SequenceType • _CollectionDefaultsType • _CollectionGeneratorDefaultsType • MutableCollectionType • Sliceable, _Sliceable • _DestructorSafeContainer
  11. 11. protocol CollectionType : SequenceType, _CollectionDefaultsType, _CollectionGeneratorDefaultsType { subscript (position: Self.Index) -> Self.Generator.Element { get } var isEmpty: Bool { get } var count: Self.Index.Distance { get } }
  12. 12. protocol SequenceType { typealias Generator : GeneratorType func generate() -> Self.Generator func underestimateCount() -> Int func map<T>(@noescape transform: (Self.Generator.Element) -> T) -> [T] func filter(@noescape includeElement: (Self.Generator.Element) -> Bool) -> [Self.Generator.Element] }
  13. 13. protocol MutableCollectionType : CollectionType { subscript (position: Self.Index) -> Self.Generator.Element { get set } }
  14. 14. protocol Sliceable : _Sliceable { typealias SubSlice : _Sliceable subscript (bounds: Range<Self.Index>) -> Self.SubSlice { get } }
  15. 15. Take a look inside Swift module for more detail
  16. 16. Apple introduced Swift 2 in WWDC 2015
  17. 17. Protocol-oriented programming in Swift
  18. 18. Protocol extension This allows you to define behavior on protocols themselves, rather than in each type’s individual conformance or in a global function.
  19. 19. protocol CustomStringConvertible { var description: String { get } } extension CustomStringConvertible { var capitalizedDescription: String { return self.description.capitalizedString } var uppercaseDescription: String { return self.description.uppercaseString } var lowercaseDescription: String { return self.description.lowercaseString } }
  20. 20. Demo
  21. 21. Protocols > Superclasses
  22. 22. Protocol extensions = magic(almost)
  23. 23. Thank you

×