子类化NSInputStream,重写委托?

时间:2016-07-19 13:15:41

标签: ios swift delegates subclass nsinputstream

我已经创建了一个NSInputStream子类,但是当它读取实际数据时,我得到以下异常。

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

我无法覆盖NSStream抽象类的以下属性:

unowned(unsafe) public var delegate: NSStreamDelegate?

这是我的继承自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) {

    }

}

这是我得到的错误: enter image description here

我做错了什么?这有可能使用Swift吗?

1 个答案:

答案 0 :(得分:1)

找到一种合适的方法来覆盖委托变量

var localdelegate: NSStreamDelegate?

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