2015-10-13 21 views
2

如果catData是一本字典,從一個休息API調用返回:手柄無論是在響應JSON字符串或INT

NSString* catId = catData[@"id"]; 

這是好的,如果catData [@「ID」]是一個字符串。不是如果它是一個int或其他東西。

我嘗試這樣做:

NSString* catId = [catData[@"id"] stringValue]; 

但導致這個:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString stringValue]: unrecognized selector sent to instance 0xa000030383130365' 

我怎樣才能妥善處理任何類型的值,並把它轉化爲我需要的字符串?

我正在使用AFNetSON和AFJSONResponseSerializer。

回答

5

而是採用

NSString* catId = [catData[@"id"] stringValue 

的情況下都使用

NSString* catId = catData[@"id"]; 

崩潰發生,因爲你正試圖轉換字符串字符串