如何初始化参数以传递到Swift的InputStream.getBuffer方法?

时间:2018-08-07 14:41:26

标签: swift stream buffer inputstream unsafemutablepointer

我正在Swift中使用InputStream。我想根据输入流的内容生成一个缓冲区(已知数据的大小很小)。但是,尝试使用getBuffer(_:length:)会返回false

 let sampleString = "Now is the time for all good men to come to the aid of their country"
 let ssData = sampleString.data(using: .utf8)!
 let ssInputStream = InputStream(data: ssData)
 ssInputStream.open()
 ...
 var buffer:UnsafeMutablePointer<UInt8>? = nil
 var length:Int = 0
 var success = ssInputStream.getBuffer(&buffer, length: &length)
 ssInputStream.close()

 // Expect
 // length == 68
 // success == true
 // buffer != nil

 // Actual
 // length == 0
 // success == false
 // buffer == nil

我已经能够使用read方法填充缓冲区,但是我有兴趣了解getBuffer的问题。

0 个答案:

没有答案