将 NSDictionary 添加到现有的 NSMutableArray - Swift

Add NSDictionary to existing NSMutableArray - Swift

一段时间以来,我一直在尝试将额外的字典值添加到 NSMutableArray 中,每个索引都包含 NSDictionaries

我尝试了下面详述的几种方法。

func findDistanceAndSortArray(offers : NSMutableArray){

    for (index, offer) in enumerate(offers) {

        var json = JSON(offer)

        if let location = json["company"]["address"]["location"].dictionary {

           //Not important Location code is here <----- 

            var distance = self.calculateDistance(newLat, longitude: newLngg)

            var newDistance = ["totalDistance" : distance]

            // I have tried these ....
            // offers.insertObject(newDistance, atIndex: index)
            // offer[newDistance]
            // json["totalDistance"] = "" <-- this inserts the dictionary but I cannot add a string into ANYOBJECT

        }
        println("MORE inside \(json)")
        println("MORE inside \(offers)")

    }
}

我得到的最接近的是使用 json["totalDistance"] = "",它将值插入到它的键旁边,但是当我尝试在字符串中添加时,它产生了一个错误,指出无法将字符串添加到值类型 JSON (因为我是用SwiftyJson解析的)

似乎无法弄清楚这一点,但我相信它很简单。

口渴:您不能在遍历同一数组时更改数组。因为你试图:

// I have tried these ....
// offers.insertObject(newDistance, atIndex: index)

解决方案应该是:

json["totalDistance"].string = distance

请尝试,我目前没有编译器可以测试它。