October 2017
Intermediate to advanced
210 pages
5h 32m
English
Protocol composition lets our types adopt multiple protocols. This is a major advantage that we get when we use protocols rather than a class hierarchy because classes, in Swift and other single-inheritance languages, can only inherit from one superclass. The syntax for protocol composition is the same as the protocol inheritance that we just saw. The following example shows how we would use protocol composition:
struct MyStruct: ProtocolOne, ProtocolTwo, Protocolthree {
// implementation here
}
Protocol composition allows us to break our requirements into many smaller components rather than inheriting all requirements from a single protocol or single superclass. This allows our type families to grow in width rather ...
Read now
Unlock full access