Marijn
Marijn

Reputation: 1479

How to use getInputStream() on NSNetService in Swift?

I’m trying to set up an NSInputStream and NSOutputStream for a Bonjour connection on a NSNetService. The getInputStream method requires arguments of the type UnsafeMutablePointer<NSInputStream?>:

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

But when I do this the inputStream and the outputStream stay nil and succes is false:

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

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

Another problem is that I cannot approach it as an ‘NSInputStream’ because it of type ‘UnsafeMutablePointer,’. When I try this I get the following error:

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

What is the right way to handle these mutable pointer types in swift? Any help would be greatly appreciated, thanks!

Upvotes: 1

Views: 851

Answers (1)

Martin R
Martin R

Reputation: 539765

Your pointers are nil because you did not allocate memory for them. But the easier solution is to pass NSIn/OutputStream? variables as inout-argument with &:

var inputStream : NSInputStream?
var outputStream : NSOutputStream?

let success = service.getInputStream(&inputStream, outputStream: &outputStream)

Upvotes: 8

Related Questions