【问题标题】:How to change dictionary keys only? Without change values swift如何仅更改字典键?无需快速更改值
【发布时间】:2019-10-16 00:10:14
【问题描述】:

我有这样的数组。

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

我们有更多的数组数据,如下所述

像这样的旅行键:

[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

在我删除一个数组 [1] 后,它看起来像: 这个

[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]]

但我想将键更新为 [0][1] 而不是 [0][2]

谁能帮我解决这个问题?

【问题讨论】:

  • 我建议使用自定义类型(例如,Trip)来表示行程数据并将tableView dataSource 创建为var array = [Trip]()
  • @Kamran 就对了。他的数据看起来像 JSON。所以他可能会创建一个Trip 结构,它可以符合Codable
  • 您可以使用enumerated() 方法,它实际上为每个元素动态添加了一个index

标签: ios arrays swift sorting dictionary


【解决方案1】:

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

如果你有这样的数组。

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

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

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

[“年”:“2021”]


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

【讨论】:

  • 我想用key检索数组数据。
  • 宾果游戏。我什至懒得自己写答案。 Mimic 是在这里使用的正确词。
  • 使用索引作为下标?数组[1]?
  • [0: ["year": "2019", "tripName": "we"] 0 是关键,只有我可以找到年份和行程以及更多详细信息。
  • @ManikandanS 您可以使用array[0] 获取["year": "2019", "tripName": "we"]
【解决方案2】:

我不太清楚这里的用例是什么,所以我尽量涵盖。


在数组上使用enumerated()

如果您只需要添加索引,请在数组上调用enumerated() 方法,例如

var array: [[String: Any]] = [["year": "2019", "tripName": "Trip#1"], ["year": "2020", "tripName": "Trip#2"], ["year": "2021", "tripName": "Trip#3"]]

array.enumerated().forEach { (arg0) in
    let (offset, element) = arg0
    debugPrint("\(offset) : \(element)")
}

这将为您可以打印或以其他方式使用的每个元素分配一个 offset,在我的示例中,控制台上的输出如下所示:

0 : ["tripName": "Trip#1", "year": "2019"]
1 : ["tripName": "Trip#2", "year": "2020"]
2 : ["tripName": "Trip#3", "year": "2021"]

如果您需要实际删除某个项目,例如

array.remove(at: 1)

并再次调用相同的 sn-p(如上),它会像这样分配 offset

0 : ["tripName": "Trip#1", "year": "2019"]
1 : ["tripName": "Trip#3", "year": "2021"]

这与您可能正在寻找的内容非常相似。


从字典映射数组

如果您实际上需要首先将字典映射到一个数组中(例如在后处理 JSON 响应期间),您也可以这样做:

var dictionary: [Int: [String: Any]] = [0: ["year": "2019", "tripName": "Trip#1"], 1: ["year": "2020", "tripName": "Trip#2"], 2: ["year": "2021", "tripName": "Trip#3"]]

var array = dictionary.map { $0.value }

然后你可以开始处理enumerated() 方法,就像我在上面向你展示的那样。


注意:显然,如果您像我一样进行映射,您将丢失原始密钥所包含的信息,并且在我的示例中将同时替换为相同的数字,这可能会产生误导,但如果您知道 offsetkey 在这里是两个完全不同的值,那么您在进行映射后可能不会有太大的惊喜。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 2023-01-27
    • 1970-01-01
    • 2016-04-26
    • 2021-02-01
    相关资源
    最近更新 更多