2021年1月2日星期六

How to create, 2D array of dictionaries and how to add, change value, append, clear etcetera in Swift

This is quite complex for me being inexperienced, I've searched and found only one reference. I'm trying to create a 2D array of dictionaries in playground.

The only shred of data I found:

https://www.dotnetperls.com/array-dictionary-swift

What I want:

var timeInOut = [Int: [String: String]]()  

Goal of my array of dictionaries:

I want to be able to have dictionaries added to the array at easily addressable addresses. Such as indexPath.row used to address dictionary. Thats why I initialized with array above, however this seems troublesome to add dictionaries automatically from table view button click for example. So the dictionaries in array must be easily addressable not created with complex strings or anything.

The above does initialize but I have no idea how to use it. The above referenced link seems to work in its configuration however it looks too verbose. Any way to shorten like I attempted below?

How do I:

-Append new empty dictionary?  -Append new dictionary with key & values?  -Append new dictionary with or without dictionary name?  -Append new dictionary value inside dictionary already established?  -Change value of key and/or value inside dictionary?  -Remove all elements of one dictionary?  -Remove all dictionaries in array?  

Here are a few lines of what I tried (shooting in the dark here):

timeInOut += [0 ["": ""]]  timeInOut[0.insert ["": ""]]  timeInOut[0.append ["": ""]].           // None of these work obviously  timeInOut.append  timeInOut.insert  timeInOut.insert[0]("", at: $1.0)  

In short I have no idea how to address this type of array and want some help :)

https://stackoverflow.com/questions/65546528/how-to-create-2d-array-of-dictionaries-and-how-to-add-change-value-append-cl January 03, 2021 at 12:03PM

没有评论:

发表评论