Objective-c Study Notes-Part 4-Protocol

Source: Internet
Author: User

** In the following content, interface refers to interfaces in the traditional sense, and @ interface refers to the type standard concept in objective-C to avoid confusion.

It's a bit strange to see this chapter. In fact, you only need to know protocal as an interface in C.

Because objective-C assigns a @ interface to each class, this concept is used to express the meaning of interface.

Abstract The purpose of the interface:

1. the implementation of similar method groups has common standards.
2. Make the "interface" independent from the class.
3. encapsulate the commonalities of classes without inheritance relationships.

If ([Assistant respondstoselector: @ selector (helpout :)]) {
[Assistant helpout: Self];
Return yes;
}
Return no;

The aboveCodeIs it possible to use reflection to determine whether an object supports a method call.

However, protocal is more powerful than interface because it can be used across applications.ProgramThis is also the reason it is called protocal.

In fact, converting "method calls" into message passing weakens the concept of the application domain, allowing multiple programs to work together seamlessly, in this regard, Apple is indeed brilliant.

Anonymous classes can also use protocal to communicate with the outside world without defining the corresponding @ interface.

How to declare a protocal:
@ Protocol protocolname
Method declarations
@ End

Note that the Protocol is not globally visible like the class name, but only saved in the namespace.

The method used in the Protocol can be declared as option (optional/unforced)

Informal protocols use @ interface to declare, such:
@ Interface nsobject (myxmlsupport)
-Initfromxmlrepresentation :( nsxmlelement *) xmlelement;
-(Nsxmlelement *) xmlrepresentation;
@ End

Unlike the @ interface corresponding to the class, informal protocols do not have the corresponding implementation file, that is, there is only one empty protocol, it can be considered as a grouping method used to calibrate a class with similar behavior.

The informal protocol does not have the type check during compilation. If the type check is required, the formal protocol should be used.

Informal protocols are often used when all methods are optional, but it is best to use formal protocols for OS X> 10.5.

About protocol objects:

The formal protocol is a special data type: it is a protocal instance rather than a class instance.

Only reference @ protocal is available at runtime.

Regarding the adoption of an agreement;

Both formal and informal protocols use the following syntax to declare:
@ Interface classname: itssuperclass <protocol list>

When multiple protocols are used:
@ Interface formatter: nsobject <formatting, prettifying>

To use a protocol class, you need to include the. h file of the Protocol, and the methods in the Protocol are not defined in the @ interface of the type itself, so as to avoid duplication.

A class can have no corresponding @ interface, but only protocal.

You can use the following test method to check whether an object complies with a protocal:
If (! [Aggreger conformstoprotocol: @ protocol (myxmlsupport)])

If you only need to check a specific method, try to use respondstoselector: Test to avoid detecting all methods in protocal.

Protocols can be inherited

Because the Protocol can be referenced by each other, in some cases, the file where the import protocol is located does not need to be:
@ Protocol B;
@ Protocol
-Foo :( id <B>) anobject;
@ End

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.