Use of Extension and Category in Objective-C

Source: Internet
Author: User

Objective-C 2.0 adds class extensions to solve two problems: allowing an object to have a private interface that can be verified by the compiler. Supports a public read-only, private writable attribute. Before Private Interface Objective-C 2.0, to define a Private function, a "Private" category: @ interface MyClass (Private) is usually declared in the implementation file) -(id) awesomePrivateMethod; @ end however, the private method of the class is usually implemented in the @ implementation block of the class, instead of implementing it in the independent @ implementation block as in the Category method above. In fact, Category only makes up for the lack of public/private restrictions in Objective-C. The real problem is that the Objective-C compiler will think that the methods declared in Category will be implemented elsewhere, so the compiler will not try to confirm whether they are actually implemented. That is to say, the methods declared by the developer may not be implemented, and the compiler will not give any warning. Compilation will assume that they will be implemented elsewhere or in an independent file. Using class exteionsion, the implementation of the declared methods and attributes will be placed in the @ implementation block of the class. Otherwise, the compiler reports an error. // SomeClass. m @ interface someClass ()-(void) extend; @ end @ implementation someClass // All implementations of Methods declared in header files or parent classes // or some private functions-(void) extend {// implement private method here;} @ end public-Readable and private writable attributes (Publicly-Readable, Privately-Writeable Properties) implement an immutable) generally, external code cannot use setter to modify the object state. However, you may want it to be a writable attribute internally. Class extensions can do this: in public interfaces (Class declaration), developers can declare an attribute to be read-only, and then declare it writable in Class extension. In this way, this attribute is read-only for external code, but internal code can use its setter method. @ Interface MyClass: NSObject @ property (retain, readonly) float value; @ end // Private extension, hidden in the main implementation file. @ interface MyClass () @ property (retain, readwrite) float value; @ end looks similar. In fact, different Class extension is often misunderstood as an anonymous category. Their syntax is indeed very similar. Although they can be used to add methods and attributes to an existing class, their purpose and behavior are different.

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.