ios Swift 中来自字节数组的 NSData
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24196820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSData from Byte array in Swift
提问by Jeff
I'm trying to create an NSData
var
from an array of bytes.
我正在尝试NSData
var
从一个字节数组创建一个。
In Obj-C I might have done this:
在 Obj-C 中,我可能已经这样做了:
NSData *endMarker = [[NSData alloc] initWithBytes:{ 0xFF, 0xD9 }, length: 2]
NSData *endMarker = [[NSData alloc] initWithBytes:{ 0xFF, 0xD9 }, length: 2]
I can't figure out a working equivalent in Swift.
我无法在 Swift 中找出等效的工作方式。
回答by Nate Cook
NSData
has an initializer that takes a bytes
pointer: init(bytes: UnsafeMutablePointer <Void>, length: Int)
. An UnsafePointer
parameter can accept a variety of different things, including a simple Swift array, so you can use pretty much the same syntax as in Objective-C. When you pass the array, you need to make sure you identify it as a UInt8
array or Swift's type inference will assume you mean to create an Int
array.
NSData
有一个初始化,需要一个bytes
指针:init(bytes: UnsafeMutablePointer <Void>, length: Int)
。一个UnsafePointer
参数可以接受各种不同的东西,包括一个简单的 Swift 数组,所以你可以使用与 Objective-C 几乎相同的语法。当您传递数组时,您需要确保将其标识为UInt8
数组,否则 Swift 的类型推断将假定您打算创建一个Int
数组。
var endMarker = NSData(bytes: [0xFF, 0xD9] as [UInt8], length: 2)
You can read more about unsafe pointer parameters in Apple's Interacting with C APIsdocumentation.
您可以在 Apple 的Interacting with C APIs文档中阅读有关不安全指针参数的更多信息。
回答by juniperi
var foo : Byte[] = [0xff, 0xD9]
var data = NSData(bytes: foo, length: foo.count)
println("\(data)")
outputs: ff d9
输出:ff d9
var data = NSData(bytes: [0xFF, 0xD9] as Byte[], length: 2)
println("\(data)")
outputs: ff d9
输出:ff d9
Edit: Ah, you have to write 'as Byte[]', so then the results are the same
编辑:啊,你必须写'as Byte []',所以结果是一样的
UPDATED for Swift 2.2
为 Swift 2.2 更新
var foo:[UInt8] = [0xff, 0xD9]
var data = NSData(bytes: foo, length: foo.count)
print("\(data)")
outputs: ff d9
输出:ff d9
var data = NSData(bytes: [0xFF, 0xD9] as [UInt8], length: 2)
print("\(data)")
outputs: ff d9
输出:ff d9
回答by vtcajones
You don't need to extend Data, in Swift 3 you can do this:
您不需要扩展数据,在 Swift 3 中您可以这样做:
let bytes:[UInt8] = [0x00, 0x01, 0x02, 0x03]
let data = Data(bytes: bytes)
print(data as NSData)
Prints "<00010203>"
打印“<00010203>”
To get the byte array again:
再次获取字节数组:
let byteArray:[UInt8] = [UInt8](data)
回答by Dmitrii Klassneckii
Swift 3 extension
斯威夫特 3 扩展
extension Data {
init<T>(fromArray values: [T]) {
var values = values
self.init(buffer: UnsafeBufferPointer(start: &values, count: values.count))
}
func toArray<T>(type: T.Type) -> [T] {
return self.withUnsafeBytes {
[T](UnsafeBufferPointer(start: let bytes:[UInt8] = [0x00, 0xf4, 0x7c]
let data = Data(fromArray: someBytes)
print(data as NSData)
let bytes = data.toArray(type: UInt8.self)
print(bytes)
, count: self.count/MemoryLayout<T>.stride))
}
}
}
Usage
用法
extension Data {
init<T>(fromArray values: [T]) {
var values = values
self.init(buffer: UnsafeBufferPointer(start: &values, count: values.count))
}
func toArray<T>(type: T.Type) -> [T] {
let value = self.withUnsafeBytes {
let data = Data(fromArray: [1, 2, 3, 4, 5])
let array = data.toArray(type: Int.self)
print(array)
// [1, 2, 3, 4, 5]
.baseAddress?.assumingMemoryBound(to: T.self)
}
return [T](UnsafeBufferPointer(start: value, count: self.count / MemoryLayout<T>.stride))
}
}
回答by abdullahselek
For Swift 5I have created another Data
extension that works well.
对于Swift 5,我创建了另一个Data
运行良好的扩展。
Sample Usage
示例用法
##代码##