The use of the OBJECTIVE-C syntax extension (Extension)

Source: Internet
Author: User

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

    1. A private read-write property can be declared, whereas a public read-only property with the same name in the declaration of a class is supported to expose read-only, privately-readable properties
    2. You can declare private methods, allow an object to have a private @interface, and be validated by the compiler
Public read-only, private writable attributes (Publicly-readable, privately-writeable properties)

One advantage of implementing an immutable (immutable) data structure is that external code cannot modify the state of an object with setter methods. However, you might want it to be a writable property inside. Extension can do this:

A property declared in the extension of a class is private and read-write, whereas a property of the same name defined in the declaration of a class is publicly read-only. In this way, the property will be read-only for external code, but the internal code can use its setter method.

Private Method

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

Baseclass+private.h

1 @interface // classification  of Classes 2 -(void) Privatemethod; 3   4 @end

However, the private method of a class is usually expected to exist in the @implementation block of the class, rather than in a separate @interface 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 Exteionsion, the implementation of the properties and methods declared therein is placed in the @implementation chunk of class. Otherwise, the compiler will make an error.

ExtensionClass.h

1 @interface // declaration  of the class 2 ReadOnly float // declaration exposes read-only properties 3   4 @end

Extensionclass.m

1 @interfaceExtensionclass ()//extension of the Class (Extension)2@property (retain, ReadWrite)floatValue//1. Declaring a private read-write property, whereas in the declaration of a class is a public read-only property with the same name3    4- (void) Privatemethod;//2. Declaring Private methods5 @end   6    7 @implementationExtensionclass//implementation of the class8    9 //implement a public method declared in a header file or parent class, or a private method declared in an extensionTen- (void) Privatemethod { One     //implement private method here;  A }    -     - @end
extension and category look similar, in fact different

Because the grammar of extension and category is very similar, extension is often misunderstood as an anonymous category, but their purpose and behavior are different.

The use of the OBJECTIVE-C syntax extension (Extension)

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.