gpt4 book ai didi

ios - 子类化 NSInputStream,重写委托(delegate)?

转载 作者:行者123 更新时间:2023-11-29 00:47:38 31 4
gpt4 key购买 nike

我创建了一个 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 可以做到这一点吗?

最佳答案

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

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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com