Subclassing NSInputStream, overriding delegate?

602 Views Asked by At

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?

1

There are 1 best solutions below

0
Zsolt Molnár On

Found a suitable way to override the delegate variable

var localdelegate: NSStreamDelegate?

override var delegate: NSStreamDelegate? {
    set {
        self.localdelegate = newValue
    }
    get {
        return self.localdelegate
    }
}