How to append elements into dictionary in swift?

2019-01-13 04:43发布

I have simple Dictionary which is defined like :

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

But later I want to add some element into this dictionary which is 3 : "efg"

But I have no idea that how can I perform this action I searched a lot on internet but nothing helps me.

Can anybody tell me how can I append 3 : "efg" into this existing dictionary?

13条回答
孤傲高冷的网名
2楼-- · 2019-01-13 05:48

SWIFT 3 - XCODE 8.1

var dictionary =  [Int:String]() 

dictionary.updateValue(value: "Hola", forKey: 1)
dictionary.updateValue(value: "Hello", forKey: 2)
dictionary.updateValue(value: "Aloha", forKey: 3)

So, your dictionary contains:

dictionary[1: Hola, 2: Hello, 3: Aloha]

查看更多
登录 后发表回答