Categories and agreements in Objective-c

Source: Internet
Author: User

The class purpose definition is similar to the definition of a class:1. Statement

@interface NSString (Csayhi) NSString is the target class; Csayhi is the name of the class, and there is no following: NSObject

2. Realize

@implementation NSString (Csayhi)

3. File name

Nsstring+csayhi

Category:

Categories also become classification (characteristics)

1, you can add a method to a known class, even if you do not have the source code of the class;

2. Adding methods through the class will become part of the original class;

3, and the method in the original class;

4, and will quilt class inheritance;

Class Purpose use:

1, can extend the class of others, can be used as the alternative way of subclasses;

2, put the same class of code into multiple files, easy to collaborate with many people.

Attention:

1, can not add the instance variable in the class;

(category, there can be no private variable, but there can be @property declaration, the property's declaration only declares the class's Set,get method (need to reference the category's header file), But the category implementation of the M file does not generate these two methods, so you use Self.xxx, will hang out, error: There is no Get/set method.

You cannot declare @synthesize in the M file of category.

In other words, there is a declaration, no implementation. )

2. You can add multiple classes for the same class, but the class name and method name cannot be duplicated.

3, do not arbitrarily rewrite the method in the class (to avoid the failure of its original program)

Extend:

Extension is a special form of the category.

1, written in the implementation of the class file;

2. The method defined in the extension is implemented in the class itself.

Role:

Defines a private method.

Attention:

Extension does not write the class name, represents the extension of the method defined, must be implemented.

If the name of the class is provided, it can be implemented selectively.

methods for extending classes:1. Modifying class source Code2. Write subclasses of this class3. Category (category)4. Extension (Extension)5. Agreement (protocal) only single inheritance is allowed in OC.
However, OC can achieve a similar multi-inheritance approach: the implementation of OC Multi-Protocol, achieve similar multi-inheritance goals.  1. Agreement2, compliance with the agreement of the class3. Calling the proxy class(An object-oriented process is a process for writing classes.) )[Email protected] (method name) protocol:a protocol is a set of criteria that declares only the method, not the implementation, that is implemented by the object receiving the protocol. The OC protocol is a list of methods that are declared by @protocolrequires other classes to be implemented, equivalent to the declarations of the @interface section. @protocol Agreement name@end Confirmation Agreement:the defined protocol must be implemented by some classes;The methods defined in the protocol need to be implemented in the class.This process is known as a confirmation agreement. @interface class Name: Parent class name < protocol name, protocol name 2,...>@endNote:the method of @required modification in the protocol should be realized when the agreement is confirmed;the method of @optional adornment can be implemented selectively. Use object Conformstoprotocol: @protocol (SALERPROTOCOL) to determine compliance with the Protocol Proxy mode:that is, he does not do the actual things, but ask others to do;plainly is to find a person to help themselves do things, equivalent to find an agent to complete their own things to do;

@interface People:person

@property (copy,nonatomic) nsstring * name;

@property (assign,nonatomic) iddelegate;

at this point, any object confirming the Marryprotocol is indicated, and delegate is the proxy.

Categories and agreements in 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.