ConstUnsafePointer很快

时间:2014-07-23 11:24:24

标签: swift

如何制作指向字符串的ConstUnsafePointer?像

这样的东西
 let someString = "abcd\r\n"
 let buff: ConstUnsafePointer<UInt8>  = ???

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

基本上我想要的是将“someString”“转换”为一种类型,我可以将其作为参数传递给我的outputStream?.write方法

2 个答案:

答案 0 :(得分:1)

您可以使用

从字符串创建UInt8数组
let buff = [UInt8](someString.utf8)

然后使用

将缓冲区写入输出流
let written = outputStream.write(buff, maxLength: buff.count)

答案 1 :(得分:0)

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