2014-07-06 69 views
0

我有問題解析從web服務的json消息中返回的字典對象。當AFNetworking成功解析它時,json是有效的。實際的響應如下:NSDictionary:無法解析[__NSCFNumber長度]:無法識別的選擇發送到實例

{ 
error = FALSE; 
"error_desc" = ""; 
images =  (
      { 
     hasItem = 0; 
     image = "https://image/php4A6Xb8"; 
     imageGroupId = 28; 
     "image_date" = "06/07/2014"; 
     "image_id" = 863; 
     tag = "MCMOBILE-06072014-033902"; 
     thumb = "https://image/thumbs/php4A6Xb8"; 
    } 
); 
} 

有了這個響應我的代碼執行以下操作:

- (void) successResponseImages: (NSDictionary *) dictionary { 

NSLog(@"Success: %@", dictionary); 

NSString *error = [ dictionary objectForKey:@"error"]; 
NSString *errorDesc = [ dictionary objectForKey:@"error_desc"]; 
NSArray *images = [ dictionary objectForKey:@"images"]; 
.... 

此時所有的值都是正確的,圖像是7個項目的數組。

我然後通過圖像使用盡量循環:

for (NSDictionary * image in images) { 
       NSLog([image objectForKey:@"image_id"]); <<<<fail 
       NSLog([image objectForKey:@"image"]); 
       NSLog([image objectForKey:@"thumb"]); 

      } 

在它失敗了炸彈,出現以下錯誤:

2014-07-06 15:44:19.161 mycobber[8976:60b] -[__NSCFNumber length]: unrecognized selector sent to instance 0x9ad2690 

我不知道是什麼問題。

編輯:更全面的堆棧跟蹤

2014-07-06 15:44:19.292 mycobber[8976:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x9ad2690' 
*** First throw call stack: 
(
0 CoreFoundation      0x0214f1e4 __exceptionPreprocess + 180 
1 libobjc.A.dylib      0x01ece8e5 objc_exception_throw + 44 
2 CoreFoundation      0x021ec243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275 
3 CoreFoundation      0x0213f50b ___forwarding___ + 1019 
4 CoreFoundation      0x0213f0ee _CF_forwarding_prep_0 + 14 
5 CoreFoundation      0x020cf89c CFStringGetLength + 140 

回答

1

的第一個參數的NSLog是一個格式字符串,但你的字典擁有一批集爲其@"image_id"密鑰。 NSLog試圖將其作爲一個字符串,通過詢問它的length,但這不起作用,因爲NSNumber沒有這樣的方法。打印單個對象的格式字符串是@"%@"

0

破解了:

NSLog([NSString stringWithFormat:@"%@",[image objectForKey:@"image_id"]]);

+1

NSLog默認採用參數,不需要'stringWithFormat:'。只要放: 'NSLog(@「%@」,image [@「image_id」]);'。簡短而簡單。 – Milo

0

你的代碼只看起來是正確的。但是,你的NSLog說法是不正確的,也沒有必要寫stringwithformat也只是簡單地寫這樣的下面,它的工作原理 -

NSLog(@"%@",[image objectForKey:@"image_id"]); 
相關問題