将应用程序从Swift2移到Swift3时,我遇到了一个错误,在尝试了几种不同的建议后,我一直无法修复。
lazy var address: AddressModel? = {
[unowned self] in
var dict = self.getpayloadDict()
var model: AddressModel
model = dict
return model
}()
model = dict throws无法分配类型为“ NSDictionary?”的值键入“ AddressModel”
AddressModel。 。
class AddressModel: Deserializable {
var City: String?
var State: String?
var PostalCode: String?
required init(data: [String: AnyObject]) {
City = data["City"] as! String?
State = data["State"] as! String?
PostalCode = data["PostalCode"] as! String?
}
}
任何帮助表示赞赏。
答案 0 :(得分:0)
该错误也应该在Swift 2中发生。很明显:getpayloadDict()
返回的字典与AddressModel
不匹配。
您可以根据字典创建一个AddressModel
实例
lazy var address: AddressModel? = { // this closure does not cause a retain cycle
let dict = self.getpayloadDict()
return AddressModel(data: dict)
}()
旁注:
as! String?
(强制将可选内容展开为可选内容)是可怕的语法。使用常规的条件下调as? String
。并且请遵循变量名以小写字母开头的命名约定。