开发者

What happens when a child of an NSArray is retained and the array is deallocated?

开发者 https://www.devze.com 2023-02-05 22:39 出处:网络
Say I read an object from an NSArray and retain that object. I am then done with the NSArray and release it. That NSArray now has a retain count of zero. Is it deallocated or will it be forced to stic

Say I read an object from an NSArray and retain that object. I am then done with the NSArray and release it. That NSArray now has a retain count of zero. Is it deallocated or will it be forced to stick around until the child object is deallocated as well?

Bas开发者_Go百科ically what I am asking if I need to copy the child instead of retaining it when the child will be kept for a while but the array isn't needed?

I am thinking that a retain is fine and the NSArray will be deallocated, but I want to make sure.


Because you've retained the object within the NSArray it (in comparison with the other objects in the array) will have a retain count of two. (One from the NSArray, and one from you.)

As such, when the NSArray is released your object will still have a retain count of one and will therefore still be around after the autorelease pool is emptied. (Until of course you release it.)

0

精彩评论

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