How to append elements into a dictionary in Swift?

后端 未结 19 1948
旧时难觅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:59

    if you want to modify or update NSDictionary then first of all typecast it as NSMutableDictionary

    let newdictionary = NSDictionary as NSMutableDictionary
    

    then simply use

     newdictionary.setValue(value: AnyObject?, forKey: String)
    

提交回复
热议问题