<分区>
标签 ios objective-c
我想用像选择器这样的队列扩展一个 NSMutableArray
- (id)dequeue {
id obj = nil;
if ([self count] > 0) {
id obj = [self objectAtIndex:0];
if (obj != nil) {
[self removeObjectAtIndex:0];
}
}
return obj;
}
问题是我启用了 ARC,obj
指向的数据在 removeObjectAtIndex:
处被释放,所以 dequeue
总是返回 null。
解决此问题的优雅方法是什么,还是我的方法完全错误?
编辑 这是由打字错误引起的,与 ARC 无关。
最佳答案
您有一个简单的范围问题。内部 obj
变量与您返回的变量不同。将您的代码更改为此,它应该可以工作
- (id)dequeue {
id obj = nil;
if ([self count] > 0) {
obj = [self objectAtIndex:0]; // removed "id" on this line since it created a new variable that and didn't assign to the one you were returning.
if (obj != nil) {
[self removeObjectAtIndex:0];
}
}
return obj;
}
关于ios - 为什么我的代码返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18893191/