将值设为
Fruit:- Apple,Banana,Mango
Flowers:- Rose,Lotus,Jasmine
Vegetables:-Tomato,Potato,Chilli
我想将dictionary
设为如下
在 Objective-C 中,我们可以将其作为:
Nsdictionary dict;
dict = @{@"Fruit" : @[@"Apple", @"Banana", @"Mango"],
@"Flowers" : @[@"Rose", @"Lotus",@"Jasmine"],
@"Vegetables" : @[@"Tomato", @"Potato",@"Chilli"],
};
如何在swift中制作相同的dictionary
?
答案 0 :(得分:1)
你可以这样做:
var dict : Dictionary = ["Fruit" : ["Apple", "Banana", "Mango"],"Flowers" : ["Rose", "Lotus","Jasmine"],"Vegetables" : ["Tomato", "Potato","Chilli"]]
您可以将其全局声明为:
var dict : [String : Array<String>] = ["Fruit" : ["Apple", "Banana", "Mango"],"Flowers" : ["Rose", "Lotus","Jasmine"],"Vegetables" : ["Tomato", "Potato","Chilli"]]
您可以这样声明空Dictonary
:
var dict : [String : Array<String>] = Dictionary()