How to append elements into a dictionary in Swift?

后端 未结 19 1975
旧时难觅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-28 23:51

    Swift 3+

    Example to assign new values to Dictionary. You need to declare it as NSMutableDictionary:

    var myDictionary: NSMutableDictionary = [:]
    let newValue = 1
    myDictionary["newKey"] = newValue
    print(myDictionary)
    

提交回复
热议问题