1

NSInputStream我正在尝试NSOutputStreamNSNetService. getInputStream 方法需要以下类型的参数UnsafeMutablePointer<NSInputStream?>

public func getInputStream(inputStream: UnsafeMutablePointer<NSInputStream?>, outputStream: UnsafeMutablePointer<NSOutputStream?>) -> Bool

但是当我这样做时 inputStream 和 outputStream 保持为零并且成功为假:

    let inputStream:UnsafeMutablePointer<NSInputStream?> = nil
    let outputStream:UnsafeMutablePointer<NSOutputStream?> = nil

    var succes = service.getInputStream(inputStream, outputStream: outputStream)

另一个问题是我不能将它作为“NSInputStream”来处理,因为它是“UnsafeMutablePointer”类型的。当我尝试这个时,我收到以下错误:

Error:(52, 13) value of type 'UnsafeMutablePointer<NSInputStream?>' (aka 'UnsafeMutablePointer<Optional<NSInputStream>>') has no member 'delegate'

快速处理这些可变指针类型的正确方法是什么?任何帮助将不胜感激,谢谢!

4

1 回答 1

8

您的指针是nil因为您没有为它们分配内存。但更简单的解决方案是将NSIn/OutputStream?变量作为 inout 参数传递&

var inputStream : NSInputStream?
var outputStream : NSOutputStream?

let success = service.getInputStream(&inputStream, outputStream: &outputStream)
于 2015-11-20T17:00:19.693 回答