相关疑难解决方法(0)

通用NSOperation子类失去了NSOperation功能

今天,当我试图"概括"我的'CoreData导入操作'时,我遇到了一个奇怪的问题.似乎如果我创建NSOperation的泛型子类,main()则不会调用func.

简单的例子:

class MyOperation<T: NSObject>: NSOperation {

    override func main() {
        println("My operation main was called")
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您创建此类的实例并将其添加到operationQueue您将看到它main()实际上没有被调用.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.operationQueue = NSOperationQueue()
    let operation = MyOperation<NSString>()
    self.operationQueue!.addOperation(operation)
}
Run Code Online (Sandbox Code Playgroud)

操作简单地从过境readyexecutingfinished状态,而无需调用main().

如果我<T: NSObject>MyOperation类中删除泛型注释,它将正常工作.

这怎么可能?我在这里错过了什么吗?

generics nsoperation nsoperationqueue swift

9
推荐指数
2
解决办法
1485
查看次数

标签 统计

generics ×1

nsoperation ×1

nsoperationqueue ×1

swift ×1