嗨,我是objC新手。我在用对象填充NSMutableArray时遇到问题。
for(id p in tmpArray){
Person *person = [[Person alloc] init];
person.usrName = p;
[persons addObject:person]; // after this line the persons
// array is still empty
[person release];
}
Persons是NSMutableArray属性,问题是它为空。是过早释放人对象还是我实例化了它错?
最佳答案
您需要使用-init
方法初始化数组,如下所示:
NSMutableArray *array = [[NSMutableArray alloc] init];
self.persons = array; // will be automatically retained
// because you're using the property
[array release]; // we alloced - we release it
不要忘记释放它:
-(void)dealloc {
self.persons = nil; // previous value of property 'persons' will be released
[super dealloc];
}
关于objective-c - 为什么我的数组为空?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2063843/