Zonily Jame Zonily Jame - 1 month ago 10
Swift Question

Append values to [String : AnyObject] or [String : String]

I want to append values to a variable of type

[String : AnyObject]
how do I do that? cause I can't do the ff.

var someObject:[String : AnyObject] = ["name":"John Diggle", "location":"Star City Prison"]

someObject.append(["work":"soldier"]) // does not work (by that I mean there is no .append function like that of other collection types such as NSMutableArray and NSMutableDictionary
someObject = someObject + ["work":"soldier"] // does not work


I'm finding a way to make easier parameters for Alamofire since Alamofire.request uses
[String : AnyObject]

Answer

You cannot append into a dictionary. Simply add new value under a new key like that:

object["Work"] = "Soldier"

Then new value added into the dictionary.