Convert Data to DispatchData in Swift 4

感情迁移 提交于 2020-01-13 19:51:14

问题


I am migrating a project to Swift 4 and I cannot figure out how I am supposed to use the new API:s to do this in Swift 4. The following code is the old Swift 3 way (from the middle of a function hence the guard):

let formattedString = "A string"
guard let stringData: Data = formattedString.data(using: .utf8) else { return }
let data: DispatchData = [UInt8](stringData).withUnsafeBufferPointer { (bufferPointer) in
    return DispatchData(bytes: bufferPointer)
}

Now it gives the following warning: init(bytes:)' is deprecated: Use init(bytes: UnsafeRawBufferPointer) instead

In order to do that you need to get access to a variable with the type UnsafeRawBufferPointer instead of UnsafeBufferPointer<UInt8>


回答1:


Use withUnsafeBytes to get an UnsafeRawPointer to the data bytes, create an UnsafeRawBufferPointer from that pointer and the count, and pass it to the DispatchData constructor:

let formattedString = "A string"
let stringData = formattedString.data(using: .utf8)! // Cannot fail!

let data = stringData.withUnsafeBytes {
    DispatchData(bytes: UnsafeRawBufferPointer(start: $0, count: stringData.count))
}

Alternatively, create an array from the Strings UTF-8 view and use withUnsafeBytes to get an UnsafeRawBufferPointer representing the arrays contiguous element storage:

let formattedString = "A string"
let data = Array(formattedString.utf8).withUnsafeBytes {
    DispatchData(bytes: $0)
}


来源:https://stackoverflow.com/questions/46346598/convert-data-to-dispatchdata-in-swift-4

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