I'm writing a file to disk and I am in the process of converting my code to Swift 3, and got stuck on something. Wanted to see if someone could give me a push in the right direction.
My current code block is:
open let text: NSString
data = possibleData ?? Data()
open let fileURL: URL?
open let fileEncoding: String.Encoding?
fileprivate let data: Data!
text = NSString(bytesNoCopy: UnsafeMutableRawPointer(mutating: data.bytes.bindMemory(to: Void.self, capacity: data.count)), length: data.count, encoding: encoding.rawValue, freeWhenDone: false)!
Swift is saying that calling data.bytes is unavailable and to I need to use .unsafebytes instead. I don't grasp the way you invoke unsafe bytes (it's not as simple as switching bytes to unsafe bytes)
So I did a little research and some people have said to use a closure block like this:
data.withUnsafeMutableBytes {(bytes: UnsafeMutablePointer<UInt8>)->Void in
//work with bytes in here
}
My problem is, what do I put inside the closure block to get my above code working? I think I'm missing something fundamentally. I can't use bytes because it gives the same error again.
Anyone have any ideas? thanks!