Swift Getting Started tutorial 17-Agreement (II)

Source: Internet
Author: User

Original blog, reproduced please indicate the source

Http://blog.csdn.net/hello_hwc

This article mainly complements the function of the Protocol not mentioned in the previous article

I. Agreements in the extension

1, through the expansion to comply with the agreement
Protocol Textprotocol {func asstring (), string}class id{var id:int = 0}extension id:textprotocol{func asString ()-> ; String{return String (ID)}}
2. By extending the Supplemental Agreement statement
Class Id{var Id:int = 0func asstring ()->string{return String (ID)}}extension Id:textprotocol


Second, the protocol type can be saved in the collection
Because the protocol itself is a type, it can be saved in the collection type.
Protocol Hwcprotocol{func logdescription ()}class obj1:hwcprotocol{func logdescription () {println ("from Obj1")}}class Obj2:hwcprotocol{func logdescription () {println ("from Obj2")}}class obj3:hwcprotocol{func logdescription () {println ( ' From Obj3 ')}}var obj1 = Obj1 () var obj2 = Obj2 () var obj3 = Obj3 () Let things: [Hwcprotocol] = [Obj1,obj2,obj3]for thing in Things {println (Thing.logdescription ())}


Each element in the array is followed by a protocol, so the methods in the Protocol are implemented.
You can call this


Third, the agreement can inherit
Similar to classes, with colons for inheritance, multiple inheritance separated by commas
Protocol Inheritingprotocol:someprotocol, Anotherprotocol {//Protocol definition}

Iv. Class-specific agreements
By adding a class to the first protocol inheritance list, the protocol can only be implemented by the class
Protocol Someclassonlyprotocol:class, Someinheritedprotocol {//Class-only protocol definition goes here}

v. Synthesis of protocols
Note: Protocol compositing does not generate a new protocol that will expire after the life cycle is exceeded
Protocol Named {var name:string {get}}protocol aged {var age:int {get}}struct person:named, aged {var NAME:STRINGV Ar age:int}//here protocol<named, aged> represents a type that follows two protocols simultaneously func Wishhappybirthday (celebrator:protocol<named, Aged >) {println ("Happy birthday \ (celebrator.name)-you ' re \ (celebrator.age)!")} Let Birthdayperson = person (name: "Malcolm", age:21) Wishhappybirthday (Birthdayperson)//output "Happy birthday Malcolm-you" Re 21

vi. consistency of agreement
Three-point rule
1, use is to determine whether the instance follows an agreement
2. Optional conversion with AS?
3. Use As for casting

VII. Provisions of optional agreements
Use @optional to declare a method or property optional implementation
Note: Optional protocols are only valid in the @objc prefix protocol
@objc indicates that the protocol is optional and that it can be exposed to objective C, and that the protocol is valid only for classes
For example
@objc protocol Counterdatasource {optional Func incrementforcount (count:int), intoptional var fixedincrement:int { Get}} @objc class Counter {var count = 0var Datasource:counterdatasource?func increment () {if let amount = DataSource?. Incrementforcount? (count) {count + = amount} else if let amount = DataSource?. Fixedincrement? {count + = Amount}init (count:int) {Self.count = Count}}}
Declaring a counter protocol, and then functional class counter completion technique, counter has an attribute count used to return the current count.
It saves the protocol type as an interface, and then checks in the increase method that the protocol implementation implements the Protocol to increase the count in the way that corresponds to the increment counter.
Then, when using the Counter class, the user selectively implements that method
Class Hwctest:counterdatasource{var Counter:counterinit (counter:counter) {self.counter = counter; Self.counter.dataSource = self;} var fixedincrement = 10func Hwcprint () {For I in 1...10{println (Counter.count) counter.increment ()}}}
Similarly, you can also use
Class Hwctest:counterdatasource{var Counter:counterinit (counter:counter) {self.counter = counter; Self.counter.dataSource = self;} Func Incrementforcount (count:int)->int{return Count+1}func Hwcprint () {For I in 1...10{println (Counter.count) Counter.increment ()}}}


Swift Getting Started tutorial 17-Agreement (II)

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.