快速数据类型-Optional <any>的强制

时间:2018-08-20 13:02:28

标签: swift coercion

我有这样的代码:

let userInfo = responseJSON.result.value as! [String : Any]
print(type(of: userInfo)) //Dictionary<String, Any>
print(type(of: userInfo["billing_addresses"]))//Optional<Any>
print(userInfo["billing_addresses"] as! [[String : AnyObject]] )
 //[["state": FL, "address_name": <null>, "city": PVD Beach, "postcode": 32082, "phone_number": 111111111, "is_default_for_billing": 1, "last_name": Rhoads, "country_code": US, "first_name": Ted, "address_line2": , "company_name": , "email_address": test6@test.com, "address_line1": 308 Sea Moss Lane]]
addNewPairToPlist(key: "billing_addresses", value: userInfo["billing_addresses"] as! [[String : AnyObject]], plistName: "UserInfo")   
// fileNotWritten
addNewPairToPlist(key: "shipping_addresses", value: userInfo["shipping_addresses"] as! [[String : AnyObject]], plistName: "UserInfo")   
//fileNotWritten



func addNewPairToPlist(key: String , value: Any?, plistName: String){
    SwiftyPlistManager.shared.addNew(value!, key: key, toPlistWithName: plistName, completion: { ( err ) in
        if err == nil {
            print("-----Value: '\(String(describing: value)) added at key: '\(key)------")
        } else {
            print(err!)
        }
    })
}

我想添加一个地址数组对象作为键-值对。我对数据类型或整个思路有误吗?) 字符串值已成功添加。 服务器的响应中可能有多个帐单/送货地址。

这是打印到控制台的内容:

[SwiftyPlistManager] Starting to add value '[["state": FL, "address_name": <null>, "city": PVD Beach, "postcode": 32082, "phone_number": 11111111, "is_default_for_billing": 1, "last_name": Rhoads, "country_code": US, "first_name": Ted, "address_line2": , "company_name": , "email_address": test6@test.com, "address_line1": 308 Sea Moss Lane]]' for key 'billing_addresses' to 'UserInfo.plist' . . .
[SwiftyPlistManager] File not written successfully
[SwiftyPlistManager] fileNotWritten
fileNotWritten

0 个答案:

没有答案