开发者

What happen when "retain" NSArray or NSMutableArray?

开发者 https://www.devze.com 2023-01-22 04:30 出处:网络
In the source codes @property(retain) NSString* str; @sythesize str; self.str = newStr; I understand actually following will happen

In the source codes

@property(retain) NSString* str;
@sythesize str;
self.str = newStr;

I understand actually following will happen

if( str 开发者_Go百科!= newStr ){
     [str release];
     str = [newStr retain]; 
}

So how about the case for NSArray or NSMutableArray ? Seem like it is complex,shallow copy and deep copy should be considered.


It's the same. Setting a property only changes the ownership of that array, not the content of the array (the content is owned by the same array). Therefore, only the array needs to be -retain'ed.

In fact, the runtime doesn't care about the specific Objective-C type of the property. The same setter procedure will be applied to every @property(retain) properties.

To make the setter perform shallow copying, make it @property(copy). There no way to make it deep-copy.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号