Protocol in Swift
What's protocol?
A protocol defines a blueprint of methods, properties,
and other requirements that suit a particular task or
piece of functionality.
The protocol can then be adopted by a class, structure,
or enumeration to provide an actual implementation of
those requirements.
Examples
struct Int
• Equatable
• Comparable
• IntegerArithmeticType, _IntegerArithmeticType
• SignedIntegerType, _SignedIntegerType
• IntegerType, _IntegerType
protocol Equatable {
func ==(lhs: Self, rhs: Self) -> Bool
}
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
}
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
}
struct Array<T>
• CollectionType
• SequenceType
• _CollectionDefaultsType
• _CollectionGeneratorDefaultsType
• MutableCollectionType
• Sliceable, _Sliceable
• _DestructorSafeContainer
protocol CollectionType : SequenceType,
_CollectionDefaultsType, _CollectionGeneratorDefaultsType {
subscript (position: Self.Index) -> Self.Generator.Element { get }
var isEmpty: Bool { get }
var count: Self.Index.Distance { get }
}
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]
}
protocol MutableCollectionType : CollectionType {
subscript (position: Self.Index) -> Self.Generator.Element { get set }
}
protocol Sliceable : _Sliceable {
typealias SubSlice : _Sliceable
subscript (bounds: Range<Self.Index>) -> Self.SubSlice { get }
}
Take a look inside
Swift module for
more detail
Apple introduced
Swift 2 in WWDC
2015
Protocol-oriented
programming in Swift
Protocol extension
This allows you to define behavior on protocols
themselves, rather than in each type’s individual
conformance or in a global function.
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
}
}
Demo
Protocols >
Superclasses
Protocol extensions = magic(almost)
Thank you

Protocol in Swift

  • 1.
  • 2.
  • 3.
    A protocol definesa blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.
  • 4.
    The protocol canthen be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.
  • 5.
  • 6.
    struct Int • Equatable •Comparable • IntegerArithmeticType, _IntegerArithmeticType • SignedIntegerType, _SignedIntegerType • IntegerType, _IntegerType
  • 7.
    protocol Equatable { func==(lhs: Self, rhs: Self) -> Bool }
  • 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.
    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.
    struct Array<T> • CollectionType •SequenceType • _CollectionDefaultsType • _CollectionGeneratorDefaultsType • MutableCollectionType • Sliceable, _Sliceable • _DestructorSafeContainer
  • 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.
    protocol SequenceType { typealiasGenerator : 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.
    protocol MutableCollectionType :CollectionType { subscript (position: Self.Index) -> Self.Generator.Element { get set } }
  • 14.
    protocol Sliceable :_Sliceable { typealias SubSlice : _Sliceable subscript (bounds: Range<Self.Index>) -> Self.SubSlice { get } }
  • 15.
    Take a lookinside Swift module for more detail
  • 16.
  • 17.
  • 18.
    Protocol extension This allowsyou to define behavior on protocols themselves, rather than in each type’s individual conformance or in a global function.
  • 19.
    protocol CustomStringConvertible { vardescription: 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.
  • 21.
  • 22.
    Protocol extensions =magic(almost)
  • 23.