简体   繁体   English

使用UInt8数组作为值的Swift字典

[英]Swift dictionary with UInt8 array as a value

Is it possible to declare a dictionary? 可以宣布字典吗? Type String as its key, and value with multiple types(String, Int, [UInt8]) ? 输入String作为其键,并使用多种类型的值(String,Int,[UInt8])?

First that came my mind: 首先想到的是:

var content:[UInt8] = [1,2,3]
var arr = [String:Any]()
arr["1"] = 1
arr["2"] = "test"
arr["3"] = content

Or you want to get values with type casted? 还是要获取类型转换的值?

UPD: Another solution in my mind was to use generics, but then you should realize your own class or protocol for storaging. UPD:在我看来,另一个解决方案是使用泛型,但是您应该实现自己的用于存储的类或协议。 And find on stack similiar solution here 并在此处找到类似的解决方案

You can do what you want using a dictionary of type [String: Any] . 您可以使用[String: Any]类型的字典来执行所需的操作。

var theDictionary = [String: Any]()
theDictionary["blue"] = "sky"
theDictionary["lucky"] = 7
theDictionary["bytes"] = [UInt8](arrayLiteral: 0x01, 0xFE, 0x54)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM