- (nullable id)myObjectAtIndex:(NSUInteger)index{
@autoreleasepool {
id value = nil;
if (index < self.count)
{
value = [self myObjectAtIndex:index];
}
return value;
}
}
I have no idea about the purpose of using autoreleasepool here. Can someone give me a hand?
Unless I'm missing the obvious, which is always possible, we can only guess:
There is a stack of autorelease pools, the top of the stack being the one in use. When the
@autoreleasepool { ... }
construct is entered a new pool is created and pushed on the stack, on exit from the construct the pool is drained and popped off the stack.The reason to create local pools is given in the
NSAutoReleasePool
docs (emphasis added):So what is the purpose in the code you are looking at? Some guesses:
Either the original author knows/believes that the called methods
count
andobjectAtIndex
(post the swizzle) add a significant amount of objects to the autorelease pool and wishes to clean these up; orThe original author was/is planning to add future code to
myObjectAtIndex
which will add a significant amount of objects to the autorelease pool and wishes to clean these up; orWishes to be able to call
objectAtIndex
and ensure there is no impact on the memory used for live objects (e.g. maybe they were measuring memory use by something else); orWho knows, accept the original author (hopefully!)
HTH