Nuno
Nuno

Reputation: 525

ConstUnsafePointer swift

How do I make a ConstUnsafePointer that points to a string? Something like

 let someString = "abcd\r\n"
 let buff: ConstUnsafePointer<UInt8>  = ???

 self.outputStream?.write(buffer:UnsafePointer<UInt8>, maxLength: <#Int#>)

So basicaly what I want isto to "convert" that "someString" to a type that after I can pass as a parameter to my outputStream?.write method

Upvotes: 0

Views: 322

Answers (2)

newacct
newacct

Reputation: 122518

let someString = "abcd\r\n"
someString.withCString { (buff: ConstUnsafePointer<Int8>) in
  // do stuff with buff in here
}

Upvotes: 0

Martin R
Martin R

Reputation: 540075

You can create an UInt8 array from the string with

let buff = [UInt8](someString.utf8)

and then write the buffer to an output stream with

let written = outputStream.write(buff, maxLength: buff.count)

Upvotes: 1

Related Questions