【发布时间】:2015-11-02 07:54:52
【问题描述】:
我收到了一系列公司的 JSON 响应。
然后我遍历数组以便将它们添加为 Company 对象。我的问题是,如果我在循环内执行[[Company alloc]init];,我将造成内存泄漏。如果我 alloc-init 退出循环,我的所有值都是相同的。最好的方法是什么?
代码如下:
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];
}
公司名称在结果数组中始终是相同的值。如果我将 Company alloc-init 放入循环中,则值是正确的。
【问题讨论】:
标签: ios objective-c nsmutablearray