• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 在 Swift 3.0 中使用 SwiftyJSONModel 接收空值

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:23:08 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

成功调用网络服务后,我得到以下 json

"balance_details":{  
         "raw_balance":"13157.70",
         "raw_credit_adeded":"4035000.00",
         "raw_credit_deducted":"0.00",
         "raw_tot_paid_amount":6000,
         "tot_paid_amount":"Rs.6000.00",
         "tot_paid_invoices":2,
         "balance":"13157.7",
         "current_balance":"Rs.13157.70",
         "credit_added":"Rs.4035000.00",
         "credit_deducted":"Rs.0.00",
         "last_recharge_amt":null,
         "raw_amount":null,
         "last_recharge_date":""
      }

对于这个网络电话,我使用的是 Alamofire。为了将此 JSON 映射到 SWIFT OBJECT,我使用了 SwiftyJSONModel 库。但我无法设置将空值存储到 JSON 对象的类型。

class BalanceDetails: JSONObjectInitializable {

    // FIXME: - properties
    var rawBalance: String
    var rawCreditAdeded: String
    var rawCreditDeducted: String
    var rawTotalPaidAmount: Int
    var totalPaidAmount: String
    var totalPaidInvoices: Int
    var balance: String
    var currentBalance: String
    var creditAdded: String
    var creditDeducted: String
    //    var lastRechargeAmount: NSObject
    //    var rawAmount: NSObject
    var lastRechargeDate: String

    // FIXME: - json properties
    enum PropertyKey: String {
        case raw_balance
        case raw_credit_adeded
        case raw_credit_deducted
        case raw_tot_paid_amount
        case tot_paid_amount
        case tot_paid_invoices
        case balance
        case current_balance
        case credit_added
        case credit_deducted
        //        case last_recharge_amt
        //        case raw_amount
        case last_recharge_date
    }

    // FIXME: - initializer
    required init(object: JSONObject<ropertyKey>) throws {
        rawBalance = try object.value(for: .raw_credit_adeded)
        rawCreditAdeded = try object.value(for: .raw_credit_adeded)
        rawCreditDeducted = try object.value(for: .raw_credit_deducted)
        rawTotalPaidAmount = try object.value(for: .raw_tot_paid_amount)
        totalPaidAmount = try object.value(for: .tot_paid_amount)
        totalPaidInvoices = try object.value(for: .tot_paid_invoices)
        balance = try object.value(for: .balance)
        currentBalance = try object.value(for: .current_balance)
        creditAdded = try object.value(for: .credit_added)
        creditDeducted = try object.value(for: .credit_deducted)
        //        lastRechargeAmount = try object.value(for: .last_recharge_amt)
        //        rawAmount = try object.value(for: .raw_amount)
        lastRechargeDate = try object.value(for: .last_recharge_date)
    }
}

当我从上面的代码中取消注释注释行时,xcode 给我一个错误,告诉我“没有'值'候选产生预期的上下文结果类型'NSObject'”。所以我的问题是如何设置该 JSON 值的类型,以便它与类字段一起存储。



Best Answer-推荐答案


我无法评论,所以我在这里回答你:

想可能是因为那些键的值是空的,所以我会试试这个:

     lastRechargeAmount = try? object.value(for: .last_recharge_amt) as? String
     rawAmount = try? object.value(for: .raw_amount) as? String

关于ios - 在 Swift 3.0 中使用 SwiftyJSONModel 接收空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43117381/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap