IOS 8:mutable property returns immutable value

Source: Internet
Author: User

The title is a bit around. For example: Declare an immutable group in the header file, such as-(Nsarray *) images, and then define a variable attribute variable in the anonymous class extension, such as @property (Nonatomic, strong) Nsmutablearray *images; //For Uiimageview. As shown below:

@interface Starimageview:uiview-(Nsarray *) images; -(void) Addimagewithname: (NSString *) imageName; @end @interface  *images;    // For Uiimageview @end

Now that the problem has come, does this not cause conflict? After all, declaring a property variable automatically declares the getter and setter methods, and the return value of the auto-declared getter method is obviously Nsmutablearray *, not nsarray *.

The answer is no. Because the property type (Nsmutablearray) is a subclass of the type (Nsarray) used by the header file, it works correctly. This means that you can use this mutable array within a class, but we want to expose through the API a seemingly immutable array of Nsarray. All people who use this code should have a consistent understanding of the API provided by this class (the Convention). If you go deep into the source and find that this property actually returns a Nsmutablearray, and then skips the API to use it directly, it will break the Convention, which is not a problem for the Class designer.

Another problem is if you use this property inside a class. It is generally recommended that you also use property access within the class, rather than directly accessing the underlying instance variable (note: This may differ from the effective objective-c, however, according to some data, such as "IOS 7 programming Combat", Apple has optimized the auto-generated getter and setter, in addition, direct access to the underlying instance variables and access getter and setter in most cases of performance loss is negligible, but, Direct access In some cases will invalidate the memory that is set in the getter, which in turn is more damaging. The reason for this is ingenious: Although the property variable is defined as the Nsmutablearray type in the class extension, the compiler declares an immutable nsarray! in the @interface of the header file that is found when processing self.images This disguised solution is different from the usual coding style, but can be used normally.

Reference documents:

Jack nutting waiting, Zhou Qingcheng and so on translation. Proficient in iOS Development (6th edition). BEIJING, People's post and telecommunications press. 211 ~ 213 Pages

IOS 8:mutable property returns immutable value

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.