The Protocol of OBJECTIVE-C

Source: Internet
Author: User

Protocol

interface is the declaration of a class header file that is not the meaning of the interface in the real sense. Protocol is the OC interface.

Role:

用来声明一些方法也就是说,一个prtocol是由一系列方法声明组成的。

Syntax format:

@protocol 协议名 //方法的声明列表@end
    • Class Compliance Agreement
      • A class can comply with 1 or more protocols
      • Any class that complies with protocol is equivalent to all method declarations that have protocol
@interface 类名 : 父类 <协议名称1, 协议名称2,…>@end

The difference between protocol and inheritance:

    • Inheritance is implemented by default, and protocol only declarations are not implemented
    • Classes of the same type may use inheritance, but classes of different types can use only protocol
    • Protocol can be used to store the declaration of a method, you can extract the common methods in multiple classes, and later let these classes adhere to the Protocol

Considerations for the Agreement:

1、协议只能声明方法,不能声明属性。2、父类遵守了某个协议,那么子类也会自动遵守协议。3、OC中可以遵循多个协议,但是只能有一个父类。OC不能多继承,但是可以多层继承。4、协议也可以遵守其他协议,然后有一个类遵守这个协议的话,就会具备所有协议的方法声明。5、每个新的协议都遵守NSObject的协议(建议都遵循,不遵循也没关系)

Protocol can implement some methods that must be implemented and selected for implementation. This is completely different from Java.

@required and @optional Keywords

    • There are 2 keywords in the protocol to control whether the method is to be implemented (by default, @required, and in most cases, the use of communication between programmers)
      • @required: This method must be implemented (if not implemented, the compiler warns)
      • @optional: This method is not necessarily implemented
@protocol SportProtocol <NSObject>@required // 如果遵守协议的类不实现会报警告- (void)playFootball;@optional // 如果遵守协议的类不实现不会报警告- (void)playBasketball;@end
注意:@required和@optional仅仅使用程序员之间交流, 并不能严格的控制某一个遵守该协议的类必 须要实现该方法, 因为即便不是实现也不会报错, 只会报一个警告
    • Type limits for protocols

      • 1, the first application scenario of the protocol, you can write the agreement to the right of the data type, explicit labeling if you want to assign a value to the variable, then the object must adhere to a protocol.
      • 2. Note: Type qualification is written to the right of the data type. (a bit like the paradigm in Java).
      • 3, although in accepting an object, the object is type-qualified (qualifying it must implement a protocol), but does not mean that the object is actually implemented the method, so each time in the invocation of the object's protocol should be validated once.

        • You can use the "Respondstoselector: @selector (method Name)" Method for this Protocol-compliant object when it is called for security validation such as:
        • Wife This class follows a protocol, implements the required method, then has a Wife object property in the person class and invokes the wife property [Self.wife cooking] in the person's own method. Here it is necessary to verify whether the wife method implements the Protocol method. (See the code below)

        • This is also used for communication between programmers. This allows the second programmer and the first programmer to develop code that is well-found in protocols that need to follow the protocol and where it needs to be implemented.

      • 4, to abide by the agreement, is only a declaration of the method of the agreement, but there is no method of implementation, all also need concrete implementation.

验证wife的方法是否实现了协议的方法。if([self.wife respondsToSelector:@selector:(cooking)]){    [self.wife cooking];}if([self.wife respondsToSelector:@selector:(washing)]){    [self.wife washing];}if([self.wife respondsToSelector:@selector:(job)]){    [self.wife job];}

Specification of the Protocol: 1. In general, the current protocol belongs to WHO, we define the agreement to whose header file 2. The name of the agreement usually begins with the class name of the class to which it belongs, followed by protocol or delegate 3. The method names in the protocol are generally preceded by the name of the Protocol protocol 4. In general, the method in the protocol passes the object that triggered the protocol.

5. In general, the name of the agent in a class is called delegate

6. When a class is to be a proxy for another class, the @protocol protocol name is typically used in. h; tells the current class that this is a protocol. The declaration of a real import agreement in. M with #import

The Protocol of OBJECTIVE-C

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.