0

I've made an NSInputStream subclass, but when it gets to read the actual data I get the following exception.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -setDelegate: only defined for abstract class.  Define -[EventusCore.FileUploadStream setDelegate:]!'

I am unable to override the the following property of NSStream abstract class:

unowned(unsafe) public var delegate: NSStreamDelegate?

Here is my class that inherits from NSInputStream

class InputStream : NSInputStream {

    private var currentStatus: NSStreamStatus  = .Closed

//    override var delegate: NSStreamDelegate?

    weak var delegate: NSStreamDelegate?

    override func open() {
        self.currentStatus = .Open
    }

    override func close() {
        self.currentStatus = .Closed
    }

    override var streamStatus: NSStreamStatus {
        return self.currentStatus
    }

    override var hasBytesAvailable: Bool {
        return self.currentStatus == .Open
    }

    // MARK: NSInputStream and CFReadStream abstract method overrides

    override func scheduleInRunLoop(aRunLoop: NSRunLoop, forMode mode: String) {

    }

    override func removeFromRunLoop(aRunLoop: NSRunLoop, forMode mode: String) {

    }

}

Here is the error I am getting: enter image description here

What am I doing wrong? Is this possible at all using Swift?

Zsolt Molnár
  • 305
  • 2
  • 8
  • Subclassing NSInputStream seems to be a (your favourite swearword here). See for example http://blog.bjhomer.com/2011/04/subclassing-nsinputstream.html (which is about Objective-C, but describes the problem) or http://www.openradar.me/19809067. – Martin R Jul 19 '16 at 13:25
  • Thanks for the typo reminder. I've read this blogpost already, but didn't any information about how to solve this specific problem. :( – Zsolt Molnár Jul 19 '16 at 13:28

1 Answers1

1

Found a suitable way to override the delegate variable

var localdelegate: NSStreamDelegate?

override var delegate: NSStreamDelegate? {
    set {
        self.localdelegate = newValue
    }
    get {
        return self.localdelegate
    }
}
Zsolt Molnár
  • 305
  • 2
  • 8