Question

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 before returning it? I believe I shouldn't, but I'm not sure...

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top