简体   繁体   English

如何从 swift 中的任何类型中获取一些数据

[英]how to get some data from the type any in swift

I have a variable with a type of Any, when I print the variable it looks like I have a json object like below:我有一个类型为 Any 的变量,当我打印该变量时,它看起来像我有一个 json object 如下所示:

var machineNumber: Any

NSLog("Machine number: \(machineNumber)")

the result is:结果是:

Machine number: {length = 29, bytes = 0xab002a05 0803073c 6b43fefe 6b3c0000 ... 00000000 00000000 }

my question is, how can I get the bytes out of the variable with the type Any.我的问题是,如何从 Any 类型的变量中获取字节。

Any help is appreciated!任何帮助表示赞赏!

This is the string format you would get for an NSData.这是您为 NSData 获得的字符串格式。 Assuming it really is an NSData, you would convert it to a Data.假设它确实是一个 NSData,你可以将它转换为一个 Data。 (And then, ideally, replace Any with Data in your definition. Any types a major pain to work with.) (然后,理想情况下,在您的定义中将Any替换为Data 。使用Any类型都是很痛苦的。)

if let data = machineNumber as? Data {
    // use `data` for the bytes
}

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

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