当我从JSON文件中获取数据时,出现错误-> EXC_BAD_ACCESS
。
我没有使用ARC,这是我的代码:
-(void)fetchedData:(NSData*)responseData
{
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
fetchedResutsArray = [json objectForKey:@"People"];
NSLog(@"%@", fetchedResutsArray);
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSDictionary *resultsDict = [fetchedResutsArray objectAtIndex:indexPath.row];
cell.textLabel.text = [resultsDict objectForKey:@"prename"];
return cell;
}
这是我的JSON结果。我从网站JSON获取数据:
{
prename = "Tim";
lastname = "Test";
username = "tim23";
},
{
prename = "John";
lastname = "Test";
username = "johniii";
},
{
prename = "Peter";
lastname = "Test";
username = "tenek23";
}
Xcode在NSLOG中显示了这一点。
fetchedResutsArray = [json objectForKey:@"People"];
这将创建一个自动释放的对象,该对象将早晚释放。您不得在该方法之外访问此对象。您违反了内存管理规则。
您可以保留该对象,这应该可以解决您的问题。
[fetchedResutsArray release]; // release old instance
fetchedResutsArray = [[json objectForKey:@"People"] retain];
或使用保留@property并使用分配对象self.fetchedResutsArray = [json objectForKey:@"People"];
。别忘了释放对象dealloc
或者只是使用ARC。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句