2013-07-27 36 views
0

我傳遞的NSString它是正確進來的數據到UITableView的,但是當我解析整數值,它表示,由於未捕獲的異常'NSInvalidArgumentException', reason: -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x744e9d0 異常終止應用程序這是我的代碼如何解析整數值到Json iOS?

(void)fetchedData:(NSData *)responseData { 
//parse out the json data 
NSError* error; 
NSDictionary* json = [NSJSONSerialization 
JSONObjectWithData:responseData options:kNilOptions error:&error]; 
    NSArray * values=[json objectForKey:@"loans"]; 


NSLog(@"Array: %@",values); 

for (NSDictionary *file in values) 
     { 
      NSNumber *fileTitle=[file objectForKey:@"id"]; 
      // NSString *fileTitle = [file objectForKey:@"sector"]; 
      [titles addObject: fileTitle]; 
     } 
[self.mainTableView reloadData]; 

} 
+0

它告訴你什麼是錯的,如果你只閱讀郵件替換

NSNumber *fileTitle=[file objectForKey:@"id"]; 

。雖然錯誤不在上面的代碼中。您正試圖將NSNumber作爲字符串對待。 JSON中的數字通常轉換爲Objective-C NSNumbers。 –

回答

0

我猜你是做這樣的事情在-tableView: cellForIndexPath

UITableViewCell *cell = [...]; 
cell.titleLabel.text = titles[indexPath.row]; 

您只能使用NSString爲-[UILabel setText:]方法。

使用簡單的-[NSNumber stringValue]或使用NSNumberFormatter將您的NSNumber轉換爲NSString。

1
[[titles addObject: fileTitle] stringValue]; 
2

您正將字符串值分配給NSNumber類型。 嘗試使用這個

NSString *fileTitle=[file objectForKey:@"id"]; 

[titles addObject: fileTitle]; 

並使用字符串值。

0

一切似乎確定。僅僅通過NSString *fileTitle = [file objectForKey:@"id"];