问题
I have a question related to this piece of code
NSNumber *lastObject = [self.myStack lastObject];
if(lastObject){
[self.myStack removeLastObject];
}
return [lastObject doubleValue];
I am surprised that the lastObject
is still in memory despite getting removed.
How is this happening? Is it that lastObject
returns a copy of the object?
回答1:
No, it's not a copy, it's just autoreleased. It will be released in the near future.
EDIT: to clarify - on [self.myStack removeLastObject]
, the object is sent release
immediately, but you got an autoreleased pointer from [self.myStack lastObject]
so the array is not the last owner of the object.
来源:https://stackoverflow.com/questions/9354215/does-lastobject-in-nsmutablearray-return-a-copy-of-the-object