| Start |
|---|
Protocol Basics About ProtocolsA Protocol specifies initialisers, properties, functions, subscripts and associated types required of a Swift object type (class, struct or enum) conforming to the protocol. In some languages similar ideas for requirement specifications of subsequent objects are known as ‘interfaces’. A declared and defined Protocol is a Type, in and of itself, with a signature of its stated requirements, somewhat similar to the manner in which Swift Functions are a Type based on their signature of parameters and returns. Swift Protocol specifications can be optional, explicitly required and/or given default implementations via a facility known as Protocol Extensions. A Swift Object Type (class, struct or enum) desiring to conform to a Protocol that’s fleshed out with Extensions for all its specified requirements needs only state its desire to conform to be in full conformance. The default implementations facility of Protocol Extensions can suffice to fulfil all obligations of conforming to a Protocol. Protocols can be inherited by other Protocols. This, in conjunction with Protocol Extensions, means Protocols can and should be thought of as a significant feature of Swift. Protocols and Extensions are important to realising Swift’s broader objectives and approaches to program design flexibility and development processes. The primary stated purpose of Swift’s Protocol and Extension capability is facilitation of compositional design in program architecture and development. This is referred to as Protocol Oriented Programming. Crusty old timers consider this superior to a focus on OOP design. Protocols (opens new window) define interfaces which can be implemented by any struct (opens new window), class (opens new window), or enum (opens new window): Properties defined in protocols (opens new window) must either be annotated as A struct, class, or enum may conform to a protocol: A protocol may also define a default implementation for any of its requirements through an extension (opens new window): A protocol can be used as a type, provided it doesn't have You may also define an abstract type that conforms to multiple protocols: With Swift 3 or better, this is done by separating the list of protocols with an ampersand ( Older versions have syntax Existing types can be extended to conform to a protocol: |
| Delegate pattern A delegate is a common design pattern used in Cocoa and CocoaTouch frameworks, where one class delegates responsibility for implementing some functionality to another. This follows a principle of separation of concerns, where the framework class implements generic functionality while a separate delegate instance implements the specific use case. Another way to look into delegate pattern is in terms of object communication. For example, A view in userinterface to display a list of data should be responsible only for the logic of how data is displayed, not for deciding what data should be displayed. Let's dive into a more concrete example. if you have two classes, a parent and a child: And you want to notify the parent of a change from the child. In Swift, delegates are implemented using a The child needs to declare a property to store the reference to the delegate: Notice the variable Also in order to mark our delegate as In this example, when the child does something and needs to notify its parent, the child will call: If the delegate has been defined, the delegate will be notified that the child has done something. The parent class will need to extend the Or using an extension: The parent also needs to tell the child that it is the child's delegate: By default a Swift For example Any methods not marked |
| Associated type requirements Protocols may define associated type requirements using the Protocols with associated type requirements can only be used as generic (opens new window) constraints: A type which conforms to the protocol may satisfy an (Note that to add clarity to this example, the generic placeholder type is named An The same goes for extensions: If the conforming type already satisfies the requirement, no implementation is needed: |
| Class-Only Protocols A protocol may specify that only a class (opens new window) can implement it through using the If a non-class type tries to implement Other protocols may inherit from the Reference semantics of class-only protocolsUsing a class-only protocol allows for reference semantics (opens new window) when the conforming type is unknown. In this example, as If Weak variables of protocol typeWhen applying the |
| Protocol extension for a specific conforming class You can write the default protocol implementation for a specific class. |
Using the RawRepresentable protocol (Extensible Enum) This struct can be extended elsewhere to add cases And an interface can design around any RawRepresentable type or specifically your enum struct At call site, you can use dot syntax shorthand for the typesafe NotificationName Using generic RawRepresentable function |
| Implementing Hashable protocol Types used in Custom type conforming to
Following example implements Note: It is not necessary that different values in custom type have different hash values, collisions are acceptable. If hash values are equal, equality operator will be used to determine real equality. RemarksA Swift protocol is a collection of requirements that conforming types must implement. The protocol may then be used in most places where a type is expected, for example Arrays and generic requirements. Protocol members always share the same access qualifier as the whole protocol, and cannot be specified separately. Although a protocol could restrict access with getter or setter requirements, as per examples above. For more information about protocols, see The Swift Programming Language (opens new window). Objective-C protocols (opens new window) are similar to Swift protocols. Protocols are also comparable to Java interfaces (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference