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