Introduction to SWIFT protocol-oriented programming

Source: Internet
Author: User
Tags instance method
The protocol defines the specifications to be followed by a group, but does not care about the internal state data of the class, nor does it care about the implementation details of the class method. It only prescribes the methods that must be provided in the class.     Classes, structs, enumerations adopt protocols by providing the methods required by the Protocol, and by the implementation of attributes.     Any type that satisfies the requirements of the Protocol is known as the compliance of the Protocol. This way of separating the specification from the implementation is the benefit of the protocol, a loosely coupled design.     The role of the agreement in Swift is equivalent to that of other languages. A protocol may require a compliance-specific instance attribute, instance method, class method, operator, or subscript.

how the protocol is defined:(similar to the definition of class, struct, enumeration)
Protocol Someprotocol {
The content
}

Structure Compliance ProtocolAfter the type is followed by a colon: The protocol name is added to the protocol. To implement multiple protocols, each protocol is separated by commas.
struct Somestructure:firstprotocol, Anotherprotocol {
Structure Body Content
}

class follows ProtocolThe same structure, but if a class has a protocol that includes the parent class, the parent class should be placed before all protocols.
Class Someclass:somesuperclass, Firstprotocol, Anotherprotocol {
The contents of a class
}

The rules for attributes: A protocol may specify that its followers provide a specific name and type of Instance PropertiesOr Class Properties。 Regardless of whether the store or the computed property is actually stored. You can also set whether the property is read-only or writable. If the protocol requires attributes to be read-write-> This property cannot be a constant or read-only computed property, and if the Protocol requires the property to be read-only-> This property is either stored or calculated to satisfy the protocol.
agreement to specify instance attributes
Protocol Someprotocol {
var musbesettable:int {Get set}/*var declares variable attributes, containing the protocol required by the instance
* The {Get set} after declaration indicates a writable
*{Get} represents read-only
*/
var doesnotneedtobesettable:int {Get}
}

agreement to specify class attributes
Protocol Anotherprotocol {
Class Var sometypeproperty:int {Get Set}//class prefix represents this property as a class member, and when implementing the Protocol in enumerations and structs, you need to use the upper static keyword as its prefix
}

The provisions of the method:A protocol may require its followers to implement certain strength methods or class methods.     These methods, as part of the protocol, place the method directly in the definition of the protocol and do not require curly braces and their method bodies. In the agreement class MethodDefinition and Class PropertiesDefinitions are similar.
Protocol Someprotocol {
Class Func Sometypemethod ()//In the protocol method prefix class to indicate that when enumerating and structuring the implementation method, you need to use static prefixes instead
}
definition of instance method in Protocol
Protocol RandomNumberGenerator {
Func random ()-> Double
}
The RandomNumberGenerator protocol does not care how the return value is generated, it only emphasizes the creation of a double type value



After introducing the definition of the methods and attributes of the protocol, briefly mention the other features of the SWIFT protocol programming.

The regulation of the mutation methodIf you sometimes need to change the owning type in the instance. In an instance method based on a value type (struct, enum), the mutating keyword is prefixed to the function, written in front of Func, indicating that the type of instance and its properties can be modified in the method. If an instance method in an instance intends to change the type of its followers instance, it is necessary to add the mutating keyword before the method in the instance definition in order for the struct, enum to adopt and satisfy the Protocol's rules for the method.

protocol TypeAlthough the protocol itself does not implement any functionality, the protocol can be used as a type. Three Usage scenarios: protocol type as a parameter type or return value type in a function, method, or constructor. The protocol type is the type of a constant, variable, or property. element types in the most arrays, dictionaries, or other containers

delegate (agent) modeA delegate is a design pattern that allows a class or struct to delegate some of the functions that are required for them to other types of instances. The implementation of a delegate pattern: defines a protocol to encapsulate the functions and methods that need to be delegated, so that its followers have these delegated methods.

to add a protocol member to an extensionExtensions can be extended to extend existing types (such as classes, structs, enumerations, and so on). Extensions can add properties, methods, subscripts, protocols, and so on for a type that already exists.
It is to be noted that all instances of the type are added to the Protocol's properties, methods, subscripts, protocols, and so on, by extending the existing type to follow the protocol.


by extending the Supplemental Agreement statementWhen a type has implemented all of the requirements in the Protocol, but is not declared, it can be extended to supplement the Protocol Declaration.

the protocol type in the collectionThe protocol type can be used by the collection to indicate that the elements in the collection are protocol types.

inheritance of a protocolA protocol can inherit one or more other protocols. syntax is similar to class inheritance, with commas separated between multiple protocols

Synthesis of ProtocolsA protocol can be composed of multiple protocols using PROTOCOL<SOMEPROTOCOL, anotherprotocol> such formats, called Protocol synthesis

consistency of inspection protocolsUse the IS and as operators to check the Protocol's consistency or conversion protocol type.

provisions on optional agreements Optional protocols contain optional members whose followers can choose whether or not to implement these members. Use the @optional keyword as a prefix in the protocol to define an optional member. You can follow the optional method name, like Someoptionalmethod? (someargument) In this way, to check that the method is implemented. Optional methods and optional properties return an optional value, and when it is inaccessible, the statement that follows does not execute and returns nil as a whole.  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.