我想像这样填充一个数组:
NSMutableArray *array = [self methodThatReturnsAnArray];
在“methodThatReturnsAnArray”方法中,我创建了一个这样的数组:
NSMutableArray *arrayInMethod = [[NSMutableArray alloc] init];
当我完成填充“arrayInMethod”时,我将返回数组,为了防止内存泄漏,我正在使用:
return [arrayInMethod autorelease];
然而,“数组”变量从未被填充。删除“autorelease”时它工作正常。我应该怎么做才能确保我释放的返回对象?
编辑
+ (NSMutableArray *)buildInstants:(NSArray *)huntsArray {
NSMutableArray *goGetObjects = [[[NSMutableArray alloc] init] autorelease];
for (int i = 0; i < [huntsArray count]; i++) {
NSDictionary *huntDict = [huntsArray objectAtIndex:i];
PHGoGet *goGet = [[PHGoGet alloc] init];
goGet.title = [huntDict objectForKey:@"title"];
goGet.description = [huntDict objectForKey:@"description"];
goGet.start = [huntDict objectForKey:@"start"];
goGet.end = [huntDict objectForKey:@"end"];
goGet.ident = [huntDict objectForKey:@"id"];
if ((CFNullRef)[huntDict objectForKey:@"image_url"] != kCFNull) {
goGet.imageURL = [huntDict objectForKey:@"image_url"];
} else {
goGet.imageURL = nil;
}
if ((CFNullRef)[huntDict objectForKey:@"icon_url"] != kCFNull) {
goGet.iconURL = [huntDict objectForKey:@"icon_url"];
} else {
goGet.iconURL = nil;
}
goGet.longitude = [huntDict objectForKey:@"lng"];
goGet.latitude = [huntDict objectForKey:@"lat"];
goGet.companyIdent = [huntDict objectForKey:@"company_id"];
[goGetObjects insertObject:goGet atIndex:i];
[goGet release];
}
return [[goGetObjects copy] autorelease];
}
最佳答案
尝试使用 NSMutableArray
的便捷方法...更改:
NSMutableArray *arrayInMethod = [[NSMutableArray alloc] init];
为了...
NSMutableArray *arrayInMethod = [NSMutableArray array];
array
将返回一个自动释放的对象。
关于iphone - 未填充自动释放的 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5981370/