成功调用网络服务后,我得到以下 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 值的类型,以便它与类字段一起存储。
我无法评论,所以我在这里回答你:
想可能是因为那些键的值是空的,所以我会试试这个:
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/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |