我一直在尝试为字典中的一个键添加多个值.
在目标c中,我们可以这样写:
NSDictionary *mapping = @{@"B": @[@"Bear",@"Buffalo"]};
但是在Swift中我们怎么写我正在尝试这样但是它不能访问:
var animalsDic = ["B": "Bear","Ball","C": "Cat","Camel" "D": "Dog","E": "Emu"]
谁能帮我吗?.
可以使用方括号在swift中创建数组:
["Bear","Ball"]
所以初始化字典的正确方法是:
var animalsDic = ["B": ["Bear","Ball"],"C": ["Cat","Camel"],"D": ["Dog"],"E": ["Emu"]]
只是想知道你在做什么,动物的类型是:
[String: [String]]
相当于:
Dictionary<String,Array<String>>