[swift-users] How to check the type of a concrete class that inherits from a generic class?

Glen Huang heyhgl at gmail.com
Sat Oct 7 01:27:34 CDT 2017


I defined some concrete classes inheriting from a generic class like this:

class Controller1: NSFetchedResultsController<NSManagedObject> {}
class Controller2: NSFetchedResultsController<NSManagedObject> {}

And I assign them a shared delegate, and in the delegate method:

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>)

I want to test the concrete type of controller, doing things differently for Controller1 and Controller2.

But doing the following gives me a warning: Cast from 'NSFetchedResultsController<NSFetchRequestResult>' to unrelated type 'Controller1’ always fails

switch controller {
case is Controller1:
        // ...

I wonder what’s the correct way to check the concrete type?


More information about the swift-users mailing list