‘withUnsafeBytes’ is deprecated

Hi. I had the following code in Swift 3 to send a String message using NSStream.

 

@objcfunc processOutput()
    {
        // Create a message and convert it to data
        let msgToServer = makeMessageToServer()
       
        self.outputBuffer = msgToServer.data(using: .utf8, allowLossyConversion: false)
       
        if self.outputBuffer != nil
        {
            if self.outputBuffer?.count != 0
            {
                let bytesWritten = self.outputBuffer!.withUnsafeBytes
                {
                    self.outputStream!.write($0, maxLength: self.outputBuffer!.count)
                }
               
                if bytesWritten <= 0
                {
                    self.error = self.outputStream?.streamError as NSError?
                }
                   
                else
                {
                    self.outputBuffer?.replaceSubrange(0..                }
            }
        }
    }

 

Now, in Swift 5, I get the following warning in Line 12:

 

'withUnsafeBytes' is deprecated: use `withUnsafeBytes(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead

 

I have read several posts on the topic but still have not figured out how to deal with the wanring in my case.

 

Your help would be greatly appreciated!

Powered by WPeMatico

You may also like...

Comments are closed.