代码之家  ›  专栏  ›  技术社区  ›  nastassia

swift数据类型-强制可选<Any>

  •  1
  • nastassia  · 技术社区  · 6 年前

    我有一个这样的代码:

    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 回复  |  直到 6 年前