声明字符串& swift中的String数组

时间:2014-12-10 10:11:08

标签: arrays swift dictionary

将值设为

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

1 个答案:

答案 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()