Objective-c Category Extension Agreement

Source: Internet
Author: User

I. Category: (category)

is to add a method on the basis of the original class, only the object that is instantiated by the original class can be used when using it.

What is the use of the category in the actual development?

Already packaged classes, (do not want to change the original class)

The team within the Division of the development of a module can use their own method of definition;

Second, the extension: (Extension) The basic concept and usage;

Using the self call method directly before xcode4.3 the error, so the extension

is to add a private method to the class, and the method declared in the extension is implemented between the @interface class name () and its corresponding @end in the class itself;

Classes sometimes require methods that are only seen by themselves, and we can define private methods of classes by extending the class;

Third, agreement: Protocol

The agreement is a treaty that is mutually adhered to, with only one declaration document;

Protocol can be implemented by any class method, the protocol itself is not a class, he is the definition of a other class can be implemented interface;

The protocol is only declaring that it is not implemented

A class that is not implemented is also called an informal agreement.

This class can be implemented or not implemented because the class is generally declared from the NSObject class.

The informal agreement is expressed in parentheses, and it is the agreement that is represented by <>;

If a subclass is to create an agreement, only if it adheres to a protocol in the base class;

in which class to import the protocol, in which class will implement the method of this Protocol,

The methods listed in the NSObject class indicate the methods that may be desired to be implemented, and if the use may be better done;

Note:

@required must enforce the implementation of the error;

@optional choose to implement the

Instance:

BOOL n = [Xuesheng respondstoselector: @selector (Night)];

if (N!=no) {

NSLog (@ "bad, no punch! ");

[Xuesheng night];

}

Else

{

NSLog (@ "Okay! ");

}

Explain:

1, respondstoselector://judge can respond to a method;

2, @selector method selector is used to select the method

3, after the judgment is a bool type, so need a bool to receive;

4, if the agreement to determine whether there is agreement;

Objective-c Category Extension Agreement

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.