你可以使用 Swift 原生类型
var dict: Dictionary<String,Array<UInt8>> = [:]
dict["first"]=[1,2,3]
print(dict) // ["first": [1, 2, 3]]
我建议您尽可能多地使用原生 Swift 类型...请参阅 Martins 对您的问题的注释,它非常有用!
如果情况是您想在那里存储任何值,只需将您的字典定义为正确的类型
var dict: Dictionary<String,Array<Any>> = [:]
dict["first"]=[1,2,3]
class C {
}
dict["second"] = ["alfa", Int(1), UInt(1), C()]
print(dict) // ["first": [1, 2, 3], "second": ["alfa", 1, 1, C]]
看,值的类型仍然是众所周知的,你可以检查一下
dict["second"]?.forEach({ (element) -> () in
print(element, element.dynamicType)
})
/*
alfa String
1 Int
1 UInt
C C
*/
如果你想存储任何值,你可以自由地做......
var type:String = "test"
var content:[UInt8] = [1,2,3,4]
var dict: Dictionary<String,Any> = [:]
dict["type"] = type
dict["content"] = content
dict.forEach { (element) -> () in // ["content": [1, 2, 3, 4], "type": "test"]
print("key:", element.0, "value:", element.1, "with type:", element.1.dynamicType)
/*
key: content value: [1, 2, 3, 4] with type: Array<UInt8>
key: type value: test with type: String
*/
}