简体   繁体   English

IOS / Objective-C:从JSON获取数字

[英]IOS/Objective-C: Get Number from JSON

I have a JSON dictionary that contains what I will call an integer (in mathematics) ie 1. 我有一个JSON字典,包含我将称为整数(在数学中),即1。

I would like to save this number to a core data attribute that is an NSInteger . 我想将此数字保存到NSInteger的核心数据属性。 The following code is issuing warning: 以下代码发出警告:

Incompatible Pointer to Integer Conversion initializing NSInteger with an expression of type 'id' 不兼容的指向整数转换的指针使用类型为“id”的表达式初始化NSInteger

 NSInteger insertID = jsonResults[@"insert_id"];

I have tried various combinations of int , NSNumber , etc. to no avail. 我尝试过各种intNSNumber等组合无济于事。 Can anyone suggest right way to do this? 谁能建议正确的方法来做到这一点?

NSDictionary can't store NSInteger . NSDictionary无法存储NSInteger It is storing NSNumber . 它存储NSNumber So you need to unwrap the NSNumber : 所以你需要解开NSNumber

NSInteger insertID = [jsonResults[@"insert_id"] integerValue];

in core data you should save numeric value as Number Type. 在核心数据中,您应将数值保存为数字类型。 For eaxample, 例如,

To save: 要保存:

insert_id = @(100)//say 100 is your insert_id value insert_id = @(100)//说100是你的insert_id值

To read: 阅读:

NSInteger insertID = [jsonResults[@"insert_id"] intValue]; NSInteger insertID = [jsonResults [@“insert_id”] intValue];

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

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