我做了一个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) {

    }

}


这是我得到的错误:
ios - 子类化NSInputStream,重写委托(delegate)?-LMLPHP

我究竟做错了什么?使用Swift完全可以吗?

最佳答案

找到了覆盖委托变量的合适方法

var localdelegate: NSStreamDelegate?

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

关于ios - 子类化NSInputStream,重写委托(delegate)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38459743/

10-09 13:02