Weitere ähnliche Inhalte Ähnlich wie Protocol in Swift (20) Mehr von Yusuke Kita (20) Kürzlich hochgeladen (20) Protocol in Swift3. A protocol defines a blueprint of methods, properties,
and other requirements that suit a particular task or
piece of functionality.
4. The protocol can then be adopted by a class, structure,
or enumeration to provide an actual implementation of
those requirements.
6. struct Int
• Equatable
• Comparable
• IntegerArithmeticType, _IntegerArithmeticType
• SignedIntegerType, _SignedIntegerType
• IntegerType, _IntegerType
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 {
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]
}
14. protocol Sliceable : _Sliceable {
typealias SubSlice : _Sliceable
subscript (bounds: Range<Self.Index>) -> Self.SubSlice { get }
}
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. 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
}
}