Swift Study Notes (III): protocols and delegates

Source: Internet
Author: User

I. Protocol | protocols

The protocol is used to define the methods and attributes required to complete certain functions. The protocol itself does not provide specific implementations of these functions, but is only used to describe these implementations. Classes, structs, and enumerations use the Protocol by providing the methods and attributes required by the Protocol. The type that can meet the Protocol requirements is called the Protocol owner.

The Protocol may require the owner to provide specific instance attributes, instance methods, class methods, operators, or subscript scripts.

// Create a protocol that declares a method speakprotocol speaker {func speak ()} // class Vicki follows the speaker protocol and implements the Speak method class Vicki in the class: speaker {func speak () {println ("Hello, I am Vicki! ")} Class animal {} // a single class in Swift can inherit a maximum of one class, in a compliance class containing a parent class, the Protocol must be placed behind the parent class (the Protocol can have multiple) class Dog: Animal, speaker {func speak () {println ("Woof! ")}}

Protocol optional method:

// Create a protocol containing the optional method. @ objc ID @ objc protocol speaker {func speak () must be added before. // declare an optional method @ optional func telljoke ()} // class Vicki does not implement the telljoke method, and no error is reported because it is an optional class Vicki: Speaker {func speak () {println ("Hello, I am Vicki! ") }}// Class Ray implements all the methods of the Protocol class Ray: Speaker {func speak () {println (" Yo, I am Ray! ")} Func telljoke () {println (" Q: Whats the object-oriented way to become wealthy? ")} // Non-Protocol method. There is no specific restriction on func writetutorial () {println (" I'm on it! ")}}

Protocol usage:

VaR Speaker: speakerspeaker = Ray () speaker. speak () // The error is reported because the speaker declares the sperker type, not Ray. Therefore, you can only call the method in the speaker protocol, instead, you cannot call the writetutorial method in Ray (although the speaker is of the ray type essentially) speaker. writetutorial () // error! // Convert the speaker type to the ray type and call the writetutorial method (speaker as Ray ). writetutorial () // because the Vicki class also complies with the speaker protocol, it can also be set as vickispeaker = Vicki () speaker. speak () // because telljoke is an optional method, you need to check whether it exists during the call. telljoke? () Speaker = dog () speaker. telljoke? ()

Use? After the method name, it is used to check whether the method exists. If the method does not exist, Nil is returned.

2. Delegate | delegates

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.