简体   繁体   English

处理JSON响应中的字符串或整数

[英]Handle either a string or int in JSON response

If catData is a dictionary returned from a rest API call: 如果catData是从rest API调用返回的字典:

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

This is fine if catData[@"id"] is a string. 如果catData [@“ id”]是字符串,则可以。 Not so much if it's an int or something else. 如果是int或其他东西,就不多了。

I tried this: 我尝试了这个:

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

But that results in this: 但这导致:

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

How can I properly handle any type of value and get it converted to the string that I need? 如何正确处理任何类型的值并将其转换为所需的字符串?

I'm using AFNetworking with AFJSONResponseSerializer. 我正在将AFNetworking与AFJSONResponseSerializer一起使用。

instead of using 而不是使用

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

both case use 两种情况下使用

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

crash happen because you are trying to convert string to string value 发生崩溃是因为您试图将字符串转换为字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM