Swift泛型类作为委托 [英] Swift Generic class as delegate
问题描述
class FetchedTableController< T:NSManagedObject> :NSFetchedResultsControllerDelegate
在课内我设置一个NSFetchedResultsController并将委托设置为self。但是由于某种原因,委托方法从未被调用。当我删除了类的通用部分时,代理人按预期的方式调用:
class FetchedTableController:NSFetchedResultsControllerDelegate
有没有办法使用泛型类作为委托?
这似乎是在Xcode7中修复的。我可以使用通用类作为NSFetchedResultsControllerDelegate,UITableViewControllerDelegate等...甚至不需要添加@objc前缀到该类。
但是,该类还必须有一些NSObject超类。
I encountered a problem when using a generic class in swift as a delegate. for example i tried to use a generic NSFetchedResultsDelegate defined as:
class FetchedTableController<T:NSManagedObject> : NSFetchedResultsControllerDelegate
Inside the class i setup a NSFetchedResultsController and set the delegate to 'self'. But for some reason the delegate methods never got called. When i removed the Generic part of the class, the delegates got called as expected:
class FetchedTableController : NSFetchedResultsControllerDelegate
Is there a solution to use generic classes as delegate?
This seems to be fixed at Xcode7. I can successfully use generic class as NSFetchedResultsControllerDelegate, UITableViewControllerDelegate etc. ... You don't even need to add the @objc prefix to the class.
However, the class has to still have some NSObject superclass.
这篇关于Swift泛型类作为委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!