Dark Horse Programmer----protocol-protocol

Source: Internet
Author: User

1. Definition of the Agreement

@protocol Agreement name <NSObject>
Method List ...
@end

2. How to comply with an agreement

1> Class Compliance Agreement
@interface Class Name: Parent class name < protocol name 1, protocol name 2>

@end



2> Protocol Compliance Agreement
@protocol protocol Name < Protocol 1, protocol name 2>

@end

3. Keywords for method declarations in the agreement

1> @required (default)
Requires the implementation method, if not implemented, there will be a warning

2> @optional
No implementation method required

4. When defining a variable, limit the object that this variable holds to a protocol

Class name < protocol name > * variable name;
ID < protocol name > variable name;

If the corresponding protocol is not followed, the compiler warns

5. Attributes declared in the @property can also be used as a limitation of compliance with the agreement

@property (Nonatomic, Strong) class name < protocol name > * attribute name;
@property (nonatomic, strong) id< protocol name > attribute name;

6. The agreement can be defined in a separate. h file, or in a class
1> If this protocol is used only in a class, the protocol should be defined in that class

2> If this protocol is used in many classes, it should be defined in a separate. h file

7. Classifications can be defined in separate. h and. m files and can also be defined in the original class
1> generally, it is defined in a separate file, because the function of classification is to extend the method without changing the original class.

2> defined in the original classification, only requires to be able to understand the grammar

Dark Horse Programmer----protocol-protocol

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.