swift 3 fill dictionary programmatically

I fill my parameters for an .post request like the following:

params = ["title":msgTitle.text ?? "",
"hashTags": [["name":"tag1"],["name":"HashTag"]]] as [String : Any]

As you can see hashTags are static. I need help understanding dictionaries. Is there a way to set hashTags via appending them to a dictionary? I want to be able to add multiple "name":"hashtagName" pairs.

Any help is appreciated!

Upvotes: 2

Views: 3191

Answers (1)

vadian
vadian

Reputation: 285240

Yes you can, for example create the dictionary

var params: [String : Any] = ["title":msgTitle.text ?? ""]

then set the value for key hashtags

params["hashTags"] = [["name":"tag1"],["name":"HashTag"]]

Or via an array

var hashtags = [["name":"tag1"]]
hashtags.append(["name":"HashTag"])
params["hashTags"] = hashtags

Or the other way round:

var params: [String : Any] = ["hashTags" : [["name":"tag1"],["name":"HashTag"]]]
params["title"] = msgTitle.text ?? ""

Upvotes: 4

Related Questions