Swift字典,数组为值

时间:2023-01-12 13:20:28

How do you declare a dictionary that has an array as the value? Is this even possible?

如何声明一个以数组作为值的字典?这有可能吗?

3 个解决方案

#1


13  

Yes

let myDictionary: [String: [Int]] = ["Hello": [1, 2, 3], "World": [4, 5, 6]]

In fact, you don't even need the explicit type declaration if you assign an initial value in place. It can go as simple as:

实际上,如果您分配了初始值,则甚至不需要显式类型声明。它可以简单到:

let myDictionary = ["Hello": [1, 2, 3], "World": [4, 5, 6]]

To use the value:

要使用该值:

println(myDictionary["Hello"][0]) // Print 1
println(myDictionary["World"][0]) // Print 4

#2


0  

 var dictionary : [String:[AnyObject]]

 var dictionary2 = [String:[AnyObject]]()

You can change AnyObject for any class or use it like AnyObject itself if you don't know the class that will be in the array.

如果您不知道将在数组中的类,您可以为任何类更改AnyObject或像AnyObject本身一样使用它。

#3


0  

If you want to store for example an array of strings:

如果要存储例如字符串数组:

var dict: [String: [String]]

or without syntactic sugar:

或没有语法糖:

var dict: Dictionary<String, Array<String>>

Dictionaries, like arrays and more generally whatever uses generics, can handle anything that is a swift type, including tuples, closures, dictionaries, dictionaries of dictionaries, arrays of dictionaries, etc. - unless conditions are specified for the generic type (for instance, a dictionary key can be any type that implements the Hashable protocol), and in that case types must conform to the constraints.

字典,如数组,更常见的是使用泛型,可以处理任何快速类型的东西,包括元组,闭包,字典,字典字典,字典数组等等 - 除非为泛型类型指定条件(例如,字典键可以是实现Hashable协议的任何类型,在这种情况下,类型必须符合约束。

#1


13  

Yes

let myDictionary: [String: [Int]] = ["Hello": [1, 2, 3], "World": [4, 5, 6]]

In fact, you don't even need the explicit type declaration if you assign an initial value in place. It can go as simple as:

实际上,如果您分配了初始值,则甚至不需要显式类型声明。它可以简单到:

let myDictionary = ["Hello": [1, 2, 3], "World": [4, 5, 6]]

To use the value:

要使用该值:

println(myDictionary["Hello"][0]) // Print 1
println(myDictionary["World"][0]) // Print 4

#2


0  

 var dictionary : [String:[AnyObject]]

 var dictionary2 = [String:[AnyObject]]()

You can change AnyObject for any class or use it like AnyObject itself if you don't know the class that will be in the array.

如果您不知道将在数组中的类,您可以为任何类更改AnyObject或像AnyObject本身一样使用它。

#3


0  

If you want to store for example an array of strings:

如果要存储例如字符串数组:

var dict: [String: [String]]

or without syntactic sugar:

或没有语法糖:

var dict: Dictionary<String, Array<String>>

Dictionaries, like arrays and more generally whatever uses generics, can handle anything that is a swift type, including tuples, closures, dictionaries, dictionaries of dictionaries, arrays of dictionaries, etc. - unless conditions are specified for the generic type (for instance, a dictionary key can be any type that implements the Hashable protocol), and in that case types must conform to the constraints.

字典,如数组,更常见的是使用泛型,可以处理任何快速类型的东西,包括元组,闭包,字典,字典字典,字典数组等等 - 除非为泛型类型指定条件(例如,字典键可以是实现Hashable协议的任何类型,在这种情况下,类型必须符合约束。