数据与 [UInt8] [英] Data vs [UInt8]
问题描述
Swift 提供了 Data
和 [UInt8]
类型,它们的作用非常相似.
Swift offers both Data
and [UInt8]
types, which do a very similar thing.
- 两者有什么区别?
- 在设计新 API 时,首选的类型是什么?
推荐答案
[UInt8]
本质上是一个字节数组,一个字节(我相信你知道),由 8 位组成.虽然 NSData 不仅仅是一个字节数组,但它的底层结构在深处是基于一个的.例如,您可以使用 data.bytes
等方法轻松地在它们之间进行转换.
[UInt8]
is essentially a byte array, a byte (as I'm sure you know), is comprised of 8 bits. Whilst NSData isn't simply a byte array, deep down it's underlying structure is based off one. You can easily convert between them using methods such as data.bytes
, for example.
在设计 API 方面,我个人建议您使用 NSData 设计它们,因为它通过简单的字节数组提供了所有额外的功能.Apple 已经为您完成了很多跑腿工作,为什么还要自己再做一次?
In terms of designing APIs, I would personally recommend you design them with NSData simply because of all the extra functionality it offers over a simple byte array. Apple has already done a lot of the legwork for you, so why do it again yourself?
这篇关于数据与 [UInt8]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!