Swift protocol and swift Protocol

Source: Internet
Author: User

Swift protocol and swift Protocol

The protocol in Swift is also called in object-c, but in other languages it may be called Interface (such as java) or pure virtual function. In fact, it all means.

The Protocol is mainly used to define uniform methods and attribute names, but does not implement any functions. Only the classes that implement the Protocol need to implement their functions.

The protocol can be understood in this way. For example, USB interfaces are common in daily life, but have you noticed that USB actually appeared very early? However, some USB-driven small fans and even some rice cookers can use USB for power-on, so, it is obvious that USB is much earlier than the production time of these small fans and rice cookers. Do we know that we will produce USB-driven fans?

Actually, it is not. Here we can compare it with our protocol. In fact, I only need to specify what method you have and what attributes you have to implement in the method, I don't need to stipulate that when you want to use USB, You need to specify the voltage to be output, whether to transmit data or what to do. I only specify which methods you have, as long as you have implemented these methods, and I don't care what functions are. In this way, we can use USB for fans, rice cookers, and even USB for bulbs and computers.


Next, let's take a look at how the protocol in Swift is defined.


Protocol MyProtocol {// protocol content // For example, here we specify a method func myProtocolFunc ()}

We can see that the specified method specifies the method name without the implemented value.


Let's look at a class that implements this protocol.

Class MyTestProtocol: MyProtocol {func myProtocolFunc () {println ("this is the protocol method ")}}


We can see that if a class wants to implement a protocol (or multiple protocols), this class can define the class name as follows: protocol name 1, protocol name 2 {}

In this way, we will find that this method is similar to the Inheritance Method, except that there are multiple inheritance protocols.

If there are inheritance and protocols, what should we do?

In fact, a lot of simplicity, we just need to inherit and write it at the beginning.


Class name: inherited class name, Protocol 1, Protocol 2 {

}

Follow this method.



Apple Development Group: 414319235 welcome to join discussions




Apple Development Group: 414319235 welcome to join discussions

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.