2013-04-27 101 views
0

我收到以下錯誤whenI啓動我的應用程序解析JSON數據到一個數組

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
    reason: '- [COViewController fetchAppNetData]: unrecognized selector 
    sent to instance 0x716d200' 

基本上我無法找出如何JSON數據解析到我的數組。我的JSON的結構如下

{ 

    "meta": {}, 

    "data": [] 

} 

我知道meta是一本字典和data是一個數組。但是,當我嘗試使用下面的代碼段,我得到上述錯誤

- (void)fetchAppNetData:(NSData *)responseData 
{ 
//parse JSON data 
NSError *error; 
NSDictionary* appNet_json = [NSJSONSerialization 
    JSONObjectWithData:responseData options:kNilOptions error:&error]; 
NSArray* appNetTimeline = [[appNet_json objectForKey:@"meta"] 
    objectForKey:@"data"]; 
NSLog(@"AppNet Timeline : %@",appNetTimeline); 
} 

如何確保我能夠識別JSON的結構正確接下來的時間,讓我能避免這類問題的?我非常抱歉想出這樣的疑惑

+0

您的錯誤與解析JSON無關。 'fetchAppNetData'沒有在您試圖用來調用它的對象中定義。你甚至從來沒有進入這個方法。 – 2013-04-27 03:04:52

+0

我打電話肯定稱其爲 'viewdidLoad' '[自performSelectorOnMainThread:@selector(fetchAppNetData)withObject:appNetData waitUntilDone:YES];' – BarryVenom 2013-04-27 03:12:15

+0

你注意到了':'對選擇名字的結束?我也沒有。 – 2013-04-27 03:13:25

回答

3

該錯誤與方法-fetchAppNetData:的內容無關。該方法甚至沒有被調用。

錯誤是說你試圖在對它沒有響應的對象上調用該名稱的方法。您已將該消息發送到類COViewController的實例,但顯然不是實現您發佈的方法的類。

+0

捕獲錯誤。我忘了在'fetchAppNetData'的末尾添加':'。這裏是更正後的代碼 '[self performSelectorOnMainThread:@selector(fetchAppNetData :) withObject:appNetData waitUntilDone:YES]; - BarryVenom 57秒前編輯' – BarryVenom 2013-04-27 03:17:08

+0

非常感謝你:) – BarryVenom 2013-04-27 03:18:24

+0

@BarryVenom - 你爲什麼要做performSelector而只是調用方法? – 2013-04-27 03:27:21