user479947
user479947

Reputation:

Initializing & navigating through Dictionaries / multi-dimensional arrays in Swift

I'm trying to wrap my head around the Swift Dictionary / multi-dimensional array syntax. I'm trying to initialize tableData and insert a new tableData.myList object using:

var tableData = ["myNumber":nil, "myList":[]]
tableData["myList"].insert(["label" : "Example Label A", "timestamp" : NSDate()], at: 0)

Which generates this error:

Value of type 'Array<Any>??' has no member 'insert'

Here's a sort of javascript sketch of what I'm trying to do: https://jsfiddle.net/L9s5amer/

Upvotes: 1

Views: 99

Answers (1)

Paul Slm
Paul Slm

Reputation: 483

You should focus on an approach more generic:

var tableData: [String: Any?] = ["myNumber": nil, "myList": [Any]()]
var value = tableData["myList"] as? [Any]
value?.insert(["label" : "Example Label A", "timestamp" : NSDate()], at: 0)
tableData["myList"] = value

Upvotes: 0

Related Questions