Swift 如何在 NSNetService 上使用 getInputStream()?
How to use getInputStream() on NSNetService in Swift?
我正在尝试为 NSNetService
上的 Bonjour 连接设置 NSInputStream
和 NSOutputStream
。 getInputStream 方法需要 UnsafeMutablePointer<NSInputStream?>
:
类型的参数
public func getInputStream(inputStream: UnsafeMutablePointer<NSInputStream?>, outputStream: UnsafeMutablePointer<NSOutputStream?>) -> Bool
但是当我这样做时,inputStream 和 outputStream 保持为 nil,succes 为 false:
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'
在swift中处理这些可变指针类型的正确方法是什么?任何帮助将不胜感激,谢谢!
您的指针是 nil
因为您没有为它们分配内存。
但更简单的解决方案是将 NSIn/OutputStream?
变量传递为
输入参数 &
:
var inputStream : NSInputStream?
var outputStream : NSOutputStream?
let success = service.getInputStream(&inputStream, outputStream: &outputStream)
我正在尝试为 NSNetService
上的 Bonjour 连接设置 NSInputStream
和 NSOutputStream
。 getInputStream 方法需要 UnsafeMutablePointer<NSInputStream?>
:
public func getInputStream(inputStream: UnsafeMutablePointer<NSInputStream?>, outputStream: UnsafeMutablePointer<NSOutputStream?>) -> Bool
但是当我这样做时,inputStream 和 outputStream 保持为 nil,succes 为 false:
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'
在swift中处理这些可变指针类型的正确方法是什么?任何帮助将不胜感激,谢谢!
您的指针是 nil
因为您没有为它们分配内存。
但更简单的解决方案是将 NSIn/OutputStream?
变量传递为
输入参数 &
:
var inputStream : NSInputStream?
var outputStream : NSOutputStream?
let success = service.getInputStream(&inputStream, outputStream: &outputStream)