开发者

Autorelease object returned from NSArray?

开发者 https://www.devze.com 2023-01-21 17:30 出处:网络
I\'m writing an NSArray category to include the -objectAtRandom message that returns an object from a random index (something similar to Python\'s choice).

I'm writing an NSArray category to include the -objectAtRandom message that returns an object from a random index (something similar to Python's choice).

Should I autorelease this object befo开发者_如何学Gore returning it? I believe I shouldn't, but I'm not sure...


According to the normal memory management rules, no, you should not. Since you're presumably using objectAtIndex: to return the object, you don't need to do any memory mangement of your own.


I'd write return [[object retain] autorelease] - this will guarantee, that even if array will be released, user will be able to work with object until current runloop cycle finish.

0

精彩评论

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