我有一個公司的數組的JSON響應。 然後我遍歷數組,以便將它們添加爲公司對象。我的問題在於,如果我在循環內做[[Company alloc]init];
,我將創建一個內存泄漏。如果alloc-init離開循環,所有的值都是一樣的。什麼是最好的方法?下面 代碼:迭代通過NSMutableArray添加數組中的對象
resultArray = [[NSMutableArray alloc]init];
responseArray = [allDataDictionary objectForKey:@"companies"];
Company *com = [[Company alloc]init];
//Looping through the array and creating the objects Movie and adding them on a new array that will hold the objects
for(int i=0;i<responseArray.count;i++){
helperDictionary =(NSDictionary*)[responseArray objectAtIndex:i];
com.title = [helperDictionary objectForKey:@"company_title"];
NSLog(@"company title %@",com.title);
[resultArray addObject:com];
}
公司標題總是結果陣列中的相同的值。如果我將公司的alloc-init放入循環中,則值是正確的。
您使用ARC嗎? – dreamlax