我想填充一個這樣的數組:自動釋放NSMutableArray裏沒有人煙
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];
}
刪除'autorelease'應該不會有任何區別,至少不會立即。你可以在'methodThatReturnsAnArray'中發佈其餘的代碼嗎? – kubi 2011-05-12 16:45:36
你在哪裏檢查數組沒有填充?返回後或稍後(可能來自按鈕處理程序)? – taskinoor 2011-05-12 16:51:56
@kubi現在我已經添加了一些代碼 – 2011-05-13 08:53:20