在UITableViewController中访问NSMutableArray崩溃

问题描述:

我是Object-c中的新成员,并且希望在Xcode 4中使用JSON数据源创建基于UITableViewController的应用程序。 我导入了JSON框架并定义了一个NSMutableArray以将其填充到响应中:在UITableViewController中访问NSMutableArray崩溃

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

[connection release]; 

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
[responseData release]; 

items = [responseString JSONValue]; 

[self.tableView reloadData]; 
} 

我一切都进行得很顺利,但是当我尝试访问我的项目数组中的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

功能,它崩溃我的应用程序。

可能是什么问题? 在此先感谢!

更新: 我修改了数组填充部分的代码,它解决了崩溃问题: NSMutableArray * a = [responseString JSONValue];

for(NSDictionary *it in a) { 

    [items addObject:it]; 
} 

但我仍然不知道为什么......

像您指定的JSON-值实例变量它semms。 对象是自动发布的(“JSONValue”不包含单词alloc,init或copy),所以它将在未来的一段时间内消失。

尝试添加属性的对象: 标题:

@property (nonatomic, retain) NSArray *items; 

实现:

@synthesize items; 

... 

self.items = [responseString JSONValue]; 

... 

- (void)dealloc { 
    ... 
    self.items = nil; 
    [super dealloc]; 
} 
+0

这工作!非常感谢! – haxpanel