Does lastObject in NSMutableArray return a copy of the object?

跟風遠走 提交于 2019-12-02 01:30:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!