lastObject是否返回一个自动释放的对象

lastObject是否返回一个自动释放的对象

本文介绍了NSArray:lastObject是否返回一个自动释放的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个iPhone项目,我想从一个NSMutableArray检索一个对象,从数组中删除对象,然后使用它。代码看起来像这样:

I'm working on an iPhone project where I would like to retrieve an object from an NSMutableArray, remove the object from the array and then use it later. The code looks something like this:

NSMutableArray * array;
// fill the array
NSObject * obj = [array lastObject];
[array removeLastObject];
// do something with obj (in the same function)

在被访问的对象上有一个retain。这是安全吗?我想假设这只会工作,如果lastObject自动释放对象,这是一个我不能弄清楚如果它。

array is the only entity with a retain on the object that is being accessed. Is this safe to do? I would assume that this would only work if lastObject autoreleased the object which is something that I can't figure out if it does.

推荐答案

为什么不调用

[array removeLastObject]

在函数的结尾?这是少一个释放/保留。

at the end of your function? That's one fewer release/retain. It might make your code more readable/less cluttered.

对于记录,:



id anObject = [[anArray objectAtIndex:0] retain];
[anArray removeObjectAtIndex:0];
[anObject someMessage];

这篇关于NSArray:lastObject是否返回一个自动释放的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:13