'bytes'不可用:改用withUnsafeBytes
以前在Swift 2.2中工作的代码现在在Swift 3中抛出下面的错误:
这是我的代码:
let tempData: NSMutableData = NSMutableData(length: 26)! tempData.replaceBytes(in: NSMakeRange(0, data.count), withBytes:data.bytes)
我应该用什么来replace“data.bytes”来修复错误? 我已经尝试过实施“withUnsafeBytes”,看看苹果公司的文档,但不能把我的头脑搞定!
假设data
具有typesData
,则以下内容应该工作:
let tempData: NSMutableData = NSMutableData(length: 26)! data.withUnsafeBytes { tempData.replaceBytes(in: NSMakeRange(0, data.count), withBytes: $0) }
使用
/// Access the bytes in the data. /// /// - warning: The byte pointer argument should not be stored and used outside of the lifetime of the call to the closure. public func withUnsafeBytes<ResultType, ContentType>(_ body: @noescape (UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType
Data
方法。 在closures$0
是一个UnsafePointer<Void>
字节(Xcode 8 beta 6中的UnsafeRawPointer
)。