ios - 如何仅更改字典键?没有 swift 改变值(value)

标签 ios arrays swift sorting dictionary

我有这样的数组。

var Array = [Int:[String:Any]]()

我只是更新值

 let updateValue = ["Name":tripName!,"date":firstDate,"year":year,"startData":startDateValue,"endDate":endDateValue,"countryName":countrname!,"totelBudjet":totelBudjet,"backgroundImage":arrImages[randomItem],"code":getAddtripeAttibutes.code,"countryIcon":countryIcon] as [String : Any]

 Array.updateValue(updateValue, forKey: tripId)

 tripId = tripId + 1

We have more Array data as mentioned below

trip keys like :

[0] [1] [2]

[0: ["year": "2019", "tripName": "Test", "backgroundImage": "Europe_Trip", "code": "DZD", "startData": 2019-05-30 10:14:11 +0000, "date": "May 30 - May 31", "endDate": 2019-05-31 10:14:11 +0000, "totelBudjet": "5000 DZD", "countryIcon": "dz", "countryName": "Algeria"], 1: ["date": "May 30 - May 31", "backgroundImage": "Europe_Trip", "endDate": 2019-05-31 10:14:43 +0000, "code": "DZD", "countryName": "Algeria", "tripName": "Gg", "countryIcon": "dz", "totelBudjet": "500 DZD", "startData": 2019-05-30 10:14:43 +0000, "year": "2019"], 2: ["year": "2019", "backgroundImage": "Asia_Trip", "endDate": 2019-05-31 10:15:00 +0000, "countryIcon": "al", "totelBudjet": "5800 ALL", "code": "ALL", "tripName": "Bb", "countryName": "Albania", "date": "May 30 - May 31", "startData": 2019-05-30 10:15:00 +0000]]

我已经用上面的数据更新了 tableview

after i delete one array [1] it look like: this

[0] [2]

 [0: ["year": "2019", "tripName": "Test", "backgroundImage": "Europe_Trip", "code": "DZD", "startData": 2019-05-30 10:14:11 +0000, "date": "May 30 - May 31", "endDate": 2019-05-31 10:14:11 +0000, "totelBudjet": "5000 DZD", "countryIcon": "dz", "countryName": "Algeria"], 2: ["year": "2019", "backgroundImage": "Asia_Trip", "endDate": 2019-05-31 10:15:00 +0000, "countryIcon": "al", "totelBudjet": "5800 ALL", "code": "ALL", "tripName": "Bb", "countryName": "Albania", "date": "May 30 - May 31", "startData": 2019-05-30 10:15:00 +0000]]

But I want to update the keys as [0][1] not as [0][2]

有人可以帮我解决这个问题吗?

最佳答案

为什么要尝试使用字典来模拟数组?

如果你有这样的数组。

var array: [[String: Any]] = [["year": "2019"], ["year": "2020"], ["year": "2021"]] // Minimalistic example array

您可以删除该特定位置的值,它会为您提供当前所需的内容。

array.remove(at: 1)
print(array[1])

["year": "2021"]


注意 - 您的数据看起来像 JSON 响应。所以,你可以使用 @Kamran在评论中的建议并使用符合 Decodable 的结构 Trip 并直接将您的数据作为 Trip 的数组获取。但是,如果您的 JSON 的格式为键 0、1、2... 那么您的 JSON 就非常糟糕,如果可以的话,您应该更改它。如果不能,则必须将字典的字典更改为字典数组或更好的结构数组。

关于ios - 如何仅更改字典键?没有 swift 改变值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56376511/

相关文章:

ios - Xcode - 使用约束来放置不完全水平居中的项目

c - 将输入中的数字加载到数组中

ios - 从字符串( View Controller 字符串名称)创建 UIViewController 类实例而不使用 Storyboards 或 NIB/XIB?

ios - 如何在全局函数中使用 AppDelegate 快速在主线程上执行选择器?

ios - 如何在 ios 中获取水中地理点的附近城市或州名称?

javascript - 如何在 Javascript 中创建如图所示的多维数组?

arrays - 当输出维度超过输入维度时,Julia `mapslices`

cocoa - Appkit/Quartz/CG 在自定义 View 中正确嵌套 NSTextView

json - 如何在 Swift 中插入 JSON 数组模型?

ios - UIBarItem - 作为方法/属性访问 customView