问题描述
我有一段与此代码有关的问题
I have a question related to this piece of code
NSNumber *lastObject = [self.myStack lastObject];
if(lastObject){
[self.myStack removeLastObject];
}
return [lastObject doubleValue];
令我惊讶的是,尽管被删除,lastObject
仍在内存中.这是怎么回事?是lastObject
返回对象的副本吗?
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?
推荐答案
否,它不是副本,而是自动发布的.它将在不久的将来发布.
No, it's not a copy, it's just autoreleased. It will be released in the near future.
要弄清-在[self.myStack removeLastObject]
上,该对象会立即发送到release
,但是您从[self.myStack lastObject]
获得了一个自动释放的指针,因此该数组不是该对象的最后所有者.
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.
这篇关于NSMutableArray中的lastObject是否返回对象的副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!