ios 如何从 NSDictionary 获取一个整数值?

来源:互联网 发布:阎连科作品知乎 编辑:程序博客网 时间:2024/05/14 20:01

我有这个奇怪的问题。NSDictionary 没有返回正确的整数值。

服务器的 JSON 响应代码。

{"status":"ok","error_code":0,"data" : [],"msg":"everything is working!"}

JSON 将被转换为 NSDictionary。

NSError *error = nil;NSDictionary *jsonDict = [NSJSONSerialization                          JSONObjectWithData:data                          options:NSJSONReadingMutableContainers                          error: &error];

我访问的 NSDictionary 值,使用下面的代码。

int error_code = (int)[jsonDict valueForKey:@"error_code"]NSLog(@"%i", error_code);The log outputs the following: 143005344

我甚至试过 objectForKey,我把同样的反应。


解决方法 1:

是的它输出的值的指针,这就是为什么你看到此日志输出。

你不能强制转换为整数的指针和期待值。

int error_code = [[jsonDict valueForKey:@"error_code"] intValue];

或者如果您想要使用现代目标 c

int error_code = [jsonDict[@"error_code"] intValue];


转载自:http://www.itstrike.cn/Question/172f23eb-78bd-4057-885d-099f691e355d.html

0 0
原创粉丝点击