简体   繁体   中英

IOS/Objective-C: Get Number from JSON

I have a JSON dictionary that contains what I will call an integer (in mathematics) ie 1.

I would like to save this number to a core data attribute that is an NSInteger . The following code is issuing warning:

Incompatible Pointer to Integer Conversion initializing NSInteger with an expression of type 'id'

 NSInteger insertID = jsonResults[@"insert_id"];

I have tried various combinations of int , NSNumber , etc. to no avail. Can anyone suggest right way to do this?

NSDictionary can't store NSInteger . It is storing NSNumber . So you need to unwrap the 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

To read:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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