Protocol (protocols)
A protocol is only a method and property that defines certain tasks or functions that are necessary.
Similar to the role of interface in Java. However, the Protocol does not implement detailed functionality.
I guess the name comes from the "contract" mentioned in Oo. But I do not think this name is very good, but it is interface the name is more acceptable. Because I think the name of the protocol is very easy and the network protocol is confusing when network programming, the network protocol is also usually referred to as protocol.
Syntax: protocol MyProtocol {//Protocol definition}
struct mystruct:myprotocol1, MyProtocol2 {...}
Class Myclass:mybaseclass, MyProtocol {...}
The agreement is written with no distinction between inheritance, so such words. Do we need to add the name of the agreement specifically to the Xxxxprotocol to differentiate whether it is an inheritance or an agreement, so that others can see clearly at a glance.
。。。。
Alas, the helpless grammar
Protocol Myclassprotocol {
class var num:int {get} //get represents a read-only property
}
struct Mystruct:myclassprotocol {//compiler will prompt//type ' mystruct ' does not//conform to protocol ' Myclassprotocol '
}
The agreement was written in class. Indicates that NUM is a class member, so it cannot be used on a struct, and if it is to be used in a struct, it is written as static, which is consistent with the previous class property and struct attribute chapter.
But interestingly enough, for protocol, he was supposed to be a strict rule. All the entries must meet its rules, however, and not necessarily. such as the {get} above, which does not write the Set method, meaning to read only, but we can still write in the class implementation of the Set method (must provide a get). But assuming that the implementation of NUM is not defined is not possible. So protocol is not strict. We simply interpret it as a condition of "at least satisfying".
The definition of a method is actually the same: protocol MyProtocol { class func MyMethod ()//class method}
Protocol MyProtocol { func myMethod ()-Int//instance method}
Protocol Mymutating { mutating func myMethod ()//mutation method (struct, enum)}
protocol Type now in addition to the basic types, enumerations. struct, class, we have one more type. Agreement.
the protocol can be used as a function parameter. The return value. Constant. variables, etc...
ProtocolMyclassprotocol {
varNum:Int {Get}
}
classMyClass1:Myclassprotocol {
varNum:Int = 1
varname ="Class1"
}
classMyClass2:Myclassprotocol {
varNum:Int = 2
varTypeName ="MyClass2"
}
classMytestclass {
varHandle: myclassprotocol //As Variable type
Init(handle: Myclassprotocol) { //as function parameters
Self.Handle= Handle
}
}
varC1 =MyClass1()
varC2 =MyClass2()
varT1 =Mytestclass(handle:C1)
vart2 =Mytestclass(handle:C2)
in this example, it can be seen that handle is more like a polymorphic application, and he is able to accept whatever an instance satisfies the Myclassprotocol protocol as a variable value.
AgentIn fact, there are so many design patterns that are not being taken out of the way, but this pattern has to be emphasized. Perhaps it is very much related to Apple's UI design architecture. The fact that this piece is talking about how Swift wants us to achieve polymorphism. Not much to say.
protocol InheritanceThis is very similar to class inheritance:protocol myprotocol:myprotocol1, myprotocol2{
}
protocol CombinationsSuppose we need to allow the type of the parameter to support very many protocols at the same time. We'll be able to use a combination of protocols. The combination of the protocol can be completed by protocol<myprotocol1, myprotocol2> form.
Official Examples:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvc3vwzxjnzxjt/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
protocol Consistency Checkin the previous notes, we used the IS and as to examine and transform the instance types of the class, and now we are able to manipulate the protocols with these two operators. Therefore, there is no need to repeat the example. (In fact, there is no difference in nature from the operations in the class.) Because the protocol is nothing more than a special form of class definition. To say a struct, it is also a special form of structure definition, as well as enumeration. )
Optional Protocol @objc protocol MyProtocol {//@objc Description This protocol is optional @optional func myFunc ()//@optional indicates that this method is optional @optional var mynum:int {get}//indicates that the variable is optional}
Optional We have said too much, just to know this can be, do not need an example.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Swift notes (19)--Protocol