The use of extension and category in Objective-c

Source: Internet
Author: User

OBJECTIVE-C 2.0 adds the class extensions to solve two problems:

    1. Allows an object to have a private interface and can be validated by the compiler.

    2. Supports a public read-only, private writable property.

Private Interface (privately Interface)

Before Objective-c 2.0, to define a private function, you would typically declare a "private" category in the implementation file:

@interface MyClass (Private)
-(ID) Awesomeprivatemethod;
@end

However, the private method of a class is often expected to be implemented in the @implementation block of the class, rather than in a separate @implementation block, as in the above category method. In fact, the category merely compensates for the objective-c lack of public/private limits.

The real problem is that the Objective-c compiler will assume that the methods declared in the category will be implemented elsewhere, so the compiler will not try to verify that they are actually implemented. In other words, the method that the developer declares may not be implemented, and the compiler will not have any warning. The compilation will assume that they will be implemented elsewhere or in separate files.

With class exteionsion, the implementation of the methods and properties declared in it will be placed in the @implementation chunk of class. Otherwise, the compiler will make an error.

[CPP] view Plaincopyprint?

  1. //SOMECLASS.M   

  2. @interface SomeClass ()

  3. -(void) extend;

  4. @end

  5. @implementation SomeClass

  6. //All declarations are implemented in the header file or in the parent class of the method   

  7. //or some private functions   

  8. -(void) Extend {

  9. //Implement Private method here;   

  10. }

  11. @end

public readable, privately writable attributes (Publicly-readable, privately-writeable properties)


A common benefit of implementing an immutable (immutable) data structure is that external code cannot modify the state of an object with a setter. However, you might want it to be a writable property inside. Class extensions can do this: in a public interface (the declaration of a Class), the developer can declare that a property is read-only and is then declared writable in the class extension. In this way, the property will be read-only for external code, but the internal code can use its setter method.

[CPP] view Plaincopyprint?

  1. @interface Myclass:nsobject

  2. @property (Retain, readonly) float value;

  3. @end

  4. //private extension, hidden in the main implementation file.   

  5. @interface MyClass ()

  6. @property (Retain, ReadWrite) float value;

  7. @end


look similar, in fact different

Class extension are often misunderstood as an anonymous category. Their syntax is indeed very similar. Although all can be used to add methods and properties to an existing class, their purpose and behavior are different.


This article is from the "just finished a movie" blog, please be sure to keep this source http://9527606.blog.51cto.com/9517606/1617600

The use of extension and category 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.