简体   繁体   中英

How to change dictionary keys only? Without change values swift

I have array like this.

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

I just update the values

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

I have updated tableview with above data

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]

Can any one please help me with this?

Why are you trying to mimic an array using a dictionary?

If you have an array like this.

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

You can delete values at that particular location and it'll give you what you need currently.

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

["year": "2021"]


Note - Your data looks like JSON response. So, you could use @Kamran 's suggestion in the comments and use a struct Trip conforming Decodable and directly get your data as an array of Trip s. However, if your JSON is formatted with keys 0, 1, 2... then your JSON is really bad and you should get it changed if you can. If you can't, you'll have to change the dictionary of dictionaries to an array of dictionary or better yet array of structs.

it is not quite clear to me what's the use-case here, so I'm trying to cover the most possible.


using enumerated() on the array

if you need only to add the index, call the enumerated() method on an array, like eg

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)")
}

that would assign an offset to each element which you can print out or use otherwise, in my example the output on the console would look like this:

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

if you need to actually remove an item, like eg

array.remove(at: 1)

and call the same snippet (above) again, it assigns the offset like this:

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

that is quite similar to what you might looking for.


mapping an array from dictionary

if you need to actually map the dictionary first into an array (like eg during post-processing a JSON response), you may go for that as well like eg:

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 }

then you can start dealing with the enumerated() method just like I shown it to you above.


NOTE: obviously if you do the mapping like I did, you will lose the information what the original key holds, and that will be replaced coincidently with the same number in my example, which may be misleading but if you are aware of that the offset and the key are two completely different values here, you may not have a huge surprise after you do the mapping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM