【问题标题】:IOS/Objective-C: Get Number from JSONIOS/Objective-C:从 JSON 中获取数字
【发布时间】:2015-11-01 15:56:03
【问题描述】:

我有一个 JSON 字典,其中包含我称之为整数(在数学中),即 1。

我想将此数字保存到核心数据属性,即NSInteger。以下代码发出警告:

不兼容的指向整数转换的指针使用“id”类型的表达式初始化 NSInteger

 NSInteger insertID = jsonResults[@"insert_id"];

我尝试了intNSNumber等的各种组合,但无济于事。任何人都可以提出正确的方法吗?

【问题讨论】:

    标签: ios objective-c integer


    【解决方案1】:

    NSDictionary 无法存储 NSInteger。它正在存储NSNumber。所以你需要解开NSNumber:

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

    【讨论】:

      【解决方案2】:

      在核心数据中,您应该将数值保存为数字类型。 例如,

      保存:

      insert_id = @(100)//说 100 是你的 insert_id 值

      阅读:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多