gpt4 book ai didi

swift - 将 NSDictionary 添加到现有的 NSMutableArray - Swift

转载 作者:行者123 更新时间:2023-11-28 09:01:38 26 4
gpt4 key购买 nike

一段时间以来,我一直在尝试在每个索引处包含 NSDictionariesNSMutableArray 中添加一个额外的字典值。

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

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

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

关于swift - 将 NSDictionary 添加到现有的 NSMutableArray - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31892366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com