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.
精彩评论