Instagram / IGListKit

A data-driven UICollectionView framework for building fast and flexible lists.
https://instagram.github.io/IGListKit/
MIT License
12.87k stars 1.54k forks source link

Cannot use Generic Section Controllers with Constrained Type Variables #455

Closed pappalar closed 7 years ago

pappalar commented 7 years ago

Hello,

I need to create multiple section controllers which differs in part of the cell setup but shares a lot of functionalities, and are all based on different subclasses of the model.

Hence I made a "abstract" Section Controller that owns a Generic "abstract" model and then every other section controller is a subclass of it, with a subclass of the generic model.

I set it up like this:


class GenericSectionController<GenericType: ParentModelClass>

var model: GenericType?

public func didUpdate(to object: Any) {
    model = object as? GenericType
  }

and

class RealSectionController: GenericSegmentSectionController<ChildModelClass>

so I am using a Constrained Type Variables in order to be able to access the model properties automatically casted to the right type in different methods, but it crashes in Adapter.m

// now that the maps have been created and contexts are assigned, we consider the section controller "fully loaded"
    for (id object in updatedObjects) {
        [[map sectionControllerForObject:object] didUpdateToObject:object];
    }

with

2   CoreFoundation                      0x0000000103203f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x0000000103119005 ___forwarding___ + 1013
    4   CoreFoundation                      0x0000000103118b88 _CF_forwarding_prep_0 + 120
    5   IGListKit                           0x0000000101a8a62d -[IGListAdapter updateObjects:] + 1405
    6   IGListKit                           0x0000000101a883dd -[IGListAdapter updateAfterPublicSettingsChange] + 170
    7   IGListKit                           0x0000000101a88250 -[IGListAdapter setDataSource:] + 88

but I can't understand if this is due to my code or if this is because the adapter is not able to cast the right controller from the generic one

zhubofei commented 7 years ago

@racer1988 Can you post a sample project? It seems to me that your SCs do not confirm to IGListSectionType protocol. But need more info to find out.

pappalar commented 7 years ago

Hi!

While preparing the sample project I managed to solve the issue which was due to how I was setting up the controllers and IGListKit was unable to access some IGListSectionType methods. so now the crash is resolved.

However I am unable to use generics to reuse the code from the parent class with a child controller type. but that is not a IGListKit question :D

Thanks, sorry for the issue

rnystrom commented 7 years ago

@racer1988 woot! Glad its resolved.

Don't worry about opening issues, others might run into the same problem so its good to have all this info in one place 😄