user3766930
user3766930

Reputation: 5829

How can I add a key and its value to a dictionary?

I have the following variable:

var params = [
    "long": lon,
    "lat": lat,
    "radius": radius,
    "deleted": "false"
]

and I would like to add a new pair to it:

"username": "blablah"

How can I do it?

I tried with:

params.setValue("blablah", forKey: "username")

but it threw error:

...this class is not key value coding-compliant for the key username.'

Upvotes: 1

Views: 46

Answers (1)

picciano
picciano

Reputation: 22701

Two issues. First, there is not enough information for the compiler to determine the dictionary type, so it should be specified. Also, use subscript notation to set the new value. See below:

var params: [String : Any] = [
    "long": lon,
    "lat": lat,
    "radius": radius,
    "deleted": "false"
]

params["username"] = "blablah"

Upvotes: 4

Related Questions