[swift-users] How to check the type of a concrete class that inherits from a generic class?
Glen Huang
heyhgl at gmail.com
Sun Oct 8 00:32:54 CDT 2017
I need to do things differently in the shared delegate based on the controller type, so this probably won’t work. But thanks, I believe it will come in handy when I do need to branch on controllers themselves.
I do have a question though, since the method is a callback, and its signature is changed (with "Thing &” added), will NSFetchedResultsController be able to find it and call it?
> On 8 Oct 2017, at 12:14 AM, C. Keith Ray <keithray at mac.com> wrote:
>
> Or make a base class for both Controller classes which defines todo () and override todo() in each Controller class.
>
> --
> C. Keith Ray
>
> * https://leanpub.com/wepntk <https://leanpub.com/wepntk> <- buy my book?
> * http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf <http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf>
> * http://agilesolutionspace.blogspot.com/ <http://agilesolutionspace.blogspot.com/>
>
> On Oct 7, 2017, at 9:12 AM, C. Keith Ray <keithray at mac.com <mailto:keithray at mac.com>> wrote:
>
>> You should be able to do this to avoid casting.(I think)
>>
>> protocol Thing {
>> func todo()
>> }
>>
>> class Controller1: NSFetchedResultsController<NSManagedObject>, Thing {
>> func todo () {doOneThing}
>> }
>> class Controller2: NSFetchedResultsController<NSManagedObject>, Thing {
>> func todo () {doAnotherThing}
>> }
>>
>> func controllerWillChangeContent(_ controller: Thing & NSFetchedResultsController<NSFetchRequestResult>) {
>> controller.todo()
>> ...
>> }
>>
>> --
>> C. Keith Ray
>>
>> * https://leanpub.com/wepntk <https://leanpub.com/wepntk> <- buy my book?
>> * http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf <http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf>
>> * http://agilesolutionspace.blogspot.com/ <http://agilesolutionspace.blogspot.com/>
>>
>> On Oct 6, 2017, at 11:28 PM, Glen Huang via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>
>>> Hi,
>>>
>>> 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:
>>> // ...
>>> default:
>>> break
>>> }
>>>
>>> I wonder what’s the correct way to check the concrete type?
>>>
>>> Regards,
>>> Glen
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org <mailto:swift-users at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171008/ab34db43/attachment.html>
More information about the swift-users
mailing list