Swift Generic class as delegate

2019-07-03 16:40发布

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条回答
▲ chillily
2楼-- · 2019-07-03 17:37

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.

查看更多
登录 后发表回答