Objective-c Protocol

Source: Internet
Author: User

Reprinted from: http://blog.eddie.com.tw/2010/12/11/protocol-in-objective-c/

We have mentioned the implicit commitment of OOP, but unlike C ++, objective-C does not have the same objective, if you want to achieve the ability of multiple types in a different type at the same time, you can implement other types of interfaces to achieve this goal. In Java/as3, "interface" is used, and in objective-C, "@ protocol" is used ". (When Java/as3 is used, be sure not to confuse interfaces with protocol. In objective-C interfaces, such as Java/as3 classes, protocol is equivalent to interface)

Let's take a look at what to do. If you want to add a custom protocol, you can add a protocol directly in your case:

Of course, you have to stay together and there is no confrontation between them. It is only for the purpose of modular structure and subsequent reusable use considerations, and it is suggested that you come up with another solution. After the addition is complete (it is a header handler), you can start to compile the program as follows:

Protocol in objective-C is used@protocolThis theory is defined. In the above program, I put two methods, but there is no internal content. Next, if I want to implement this Protocol, all methods defined in @ protocol must be implemented. In addition, in objective-C
After 2.0@requiredAnd@optionalYou can set whether this method must be implemented. The usage is as follows:

If it is not specified, the setting is @ required. If you want to implement this protocol, literally speaking, the @ required part must be implemented. @ optional indicates that you are too high. It should be noted that @ required and @ optional affect the two methods,Is from all the methods below it will be affected until another direve ve or @ endSo if you want to omit @ required, remember that the methods should be placed before @ optional. Next let's take a look at how to implement this Protocol:

To implement protocol, use the "<>" tag to mark the name of protocol. It is not limited that only one protocol can be implemented. If you want to implement multiple protocols, you can use the following to split the nodes:

Because we haven't implemented the method defined in Protocol till now, if we press build directly at this time, the warning message is displayed:

Next, fill in the required information. Because the method has been defined in the Protocol, you do not need to perform this operation again in the @ interface, you only need to compile the actual method in @ implementation.

If you have implemented all the @ required methods, it is strange to follow the protocol (conform) or adopt (hard Translation into Chinese, in English ). To check whether an object complies with the rules of a protocol:

Protocol itself can also be similar to other protocols, for example:

In this case, if you want to implement Protocol B, both methoda and methodb must be implemented.

In addition, you can use protocol as a general type, for example:

It indicates that this some_object is an object with the protocol as drawable. You can check the protocol in the dynamic route segment first. Of course, you can also have more than one at a time. In the same way, you can split them with funny points:

The above mentioned method is fixed by @ protocol.formal
protocol
From the perspective of name, it is also calledinformal
protocol
But this will be explained again in the category section.

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.