How to append elements into a dictionary in Swift?

后端 未结 19 1973
旧时难觅i
旧时难觅i 2020-11-28 23:31

I have a simple Dictionary which is defined like:

var dict : NSDictionary = [ 1 : \"abc\", 2 : \"cde\"]

Now I want to add an element into t

19条回答
  •  臣服心动
    2020-11-29 00:04

    Dict.updateValue updates value for existing key from dictionary or adds new new key-value pair if key does not exists.

    Example-

    var caseStatusParams: [String: AnyObject] = ["userId" : UserDefault.userID ]
    caseStatusParams.updateValue("Hello" as AnyObject, forKey: "otherNotes")
    

    Result-

    ▿  : 2 elements
        - key : "userId"
        - value : 866
    ▿  : 2 elements
        - key : "otherNotes"
        - value : "Hello"
    

提交回复
热议问题