2011-09-30 77 views
0

我是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]; 
} 

但我仍然不知道爲什麼......

回答

0

像您指定的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