Ios 解析后如何在字典中追加值";jSON";swift中的数据?

Ios 解析后如何在字典中追加值";jSON";swift中的数据?,ios,json,swift,google-places-api,Ios,Json,Swift,Google Places Api,这是我的密码 func connectionDidFinishLoading(connection: NSURLConnection){ var err: NSError var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDict

这是我的密码

func connectionDidFinishLoading(connection: NSURLConnection){

    var err: NSError
    var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary

    if jsonResult.count>0 && jsonResult["results"]!.count>0 {
        var result: NSArray = jsonResult["results"] as! NSArray
        println("\(result)")
        var dict = NSDictionary()
        var myDict = NSDictionary()

        for dict in result {
            let googleGeo = dict["geometry"] as! NSDictionary
            let googleLoc = googleGeo["location"] as! NSDictionary
            let latitude = googleLoc["lat"] as! Float
            let longitude = googleLoc["lng"] as! Float
            let googleicon = dict.valueForKey("icon") as? NSString
            let googlename = dict["name"] as? NSString
            let googlevicinity = dict["vicinity"] as? NSString

            myDict.setValue(latitude, forKey: "lat"    
        }
    }
}
在从GooglePlacesAPI解析之后,我收到了经度、纬度、名称、邻近区域和图标。现在,我想将这些值附加到myDctionary,以便将该值传递给数组和下一个视图控制器


请有人告诉我怎么做吗?

我的朋友,你应该试试这个

让字典确切地知道您希望它保存哪种类型的键/值对。您希望使用“字符串”作为键,并且由于您的值具有不同的数据类型,因此您希望使用“AnyObject”作为值

更新对于Swift 2,您将使用String:AnyObject,但对于Swift 3,您将使用String:Any。我更新了代码以显示Swift 3版本

//this says your dictionary will accept key value pairs as String/Any
var myDict = [String:Any]()
使用Any是因为您有太多不同的数据类型

//Your values have are being cast as NSDictionary, Float, and NSStrings. All different datatypes
let googleGeo = dict["geometry"] as? NSDictionary
let googleLoc = googleGeo["location"] as? NSDictionary
let latitude = googleLoc["lat"] as? Float
let longitude = googleLoc["lng"] as? Float
let googleicon = dict.valueForKey("icon") as? NSString
let googlename = dict["name"] as? NSString
let googlevicinity = dict["vicinity"] as? NSString
现在您可以使用方法.updateValue(value:value,forKey:Hashable)来设置键和值

//update the dictionary with the new values with value-type Any and key-type String
myDict.updateValue(googleGeo, forKey: "geometry")
myDict.updateValue(googleLoc, forKey: "location")
myDict.updateValue(latitude, forKey: "lat")
myDict.updateValue(longitude, forKey: "lng")
myDict.updateValue(googleicon, forKey: "icon")
myDict.updateValue(googlename, forKey: "name")
myDict.updateValue(googlevicinity, forKey: "vicinity")
myDict现在应该拥有所有这些键/值对,您可以通过使用键提取值来对它们执行所需的操作。顺便说一句,我只使用了那些关键的名字,因为在你的帖子中,这似乎是你使用的惯例。你可以随意给钥匙命名。但是无论你给它们起什么名字,它们都必须是你用来提取值的同一个名字


希望这有帮助

你能详细说明一下你想在哪本词典中附加什么吗?还有,到目前为止你自己尝试过什么?进一步说:不要强制_cast,尽量避免使用objc函数。你想传递lat、log、name和neighboration值的值是什么,我需要存储在我的字典中??