Swift Generic class as delegate

2019-07-03 16:37发布

问题:

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?

回答1:

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.