Data vs [UInt8]

感情迁移 提交于 2020-01-14 10:34:07

问题


Swift offers both Data and [UInt8] types, which do a very similar thing.

  • What are the differences between the two?
  • When designing new API's, what's the preferred type?

回答1:


[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.

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?



来源:https://stackoverflow.com/questions/39873282/data-vs-uint8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!