1

I have a generic view controller that is used as a base controller for other controllers. Generic type of that base controller is used to determine which type of data is used like so:

public class BaseViewController<T where T: ExampeDataProtocol> : UIViewController {
    var data: T?
}

For some reason, when I'm setting my self.data in subclass view controller I get an error.

public class SubClassViewController: BaseViewController<SomeData> {

}

When I debugged, before setting that value, self.data was already set and it was UIView with CGRect(0, 0, 600, 600). How is this even possible?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
zhuber
  • 5,364
  • 3
  • 30
  • 63
  • I can't see your code or know what you're doing, but if I had to guess, I'd say this is probably an abuse of generics. – nhgrif Feb 04 '16 at 12:37
  • For some reason after I set self.data = something in SubclassViewController, and then immediately print(self.view) it prints out "". Before setting it is normal UIView... Like setting self.data is causing controller view to become that type... – zhuber Feb 04 '16 at 12:39
  • Don't make things generic that are supposed to be specific. ViewControllers are in essence unique. If you notice you have lots of duplicate code in a couple of VC's, that code probably doesn't belong in a VC but in a separate class / struct. – R Menke Feb 04 '16 at 12:41
  • 1
    As addition to R Menke:s comment: if you have lots of duplicate code, and you can't fit it in a separate class/struct, make use of a protocol with a default implementation and let your "look-alike" view controller conform to that protocol. – dfrib Feb 04 '16 at 12:43
  • @dfri also a good option, there are so many good ways to reduce duplicate code in ViewControllers and at the same time reduce code smell. Generics is definitely not one of them. – R Menke Feb 04 '16 at 12:46
  • I have 5 VCs that inherit from that BaseViewController, and just by passing that generic type of ViewModel I save my self up to 200 lines of code per controller. I don't think its an abuse of generic, just can't figure out why is my view of view controller affected by this... – zhuber Feb 04 '16 at 12:46
  • Could you explain how do you construct the view for the BaseViewController? Do you use storyboards or do it programmatically? Maybe the generics are not the root cause? – Jan Weinkauff Feb 04 '16 at 12:53
  • Its from xib, with super.init(nibName:bundle)... – zhuber Feb 04 '16 at 12:56
  • I struggle to understand what the error is you try to solve. Do you get an runtime error or is it the unexpected dimensions of the view or something else? Maybe you can provide more context and explain what the expect state of the view controller/view should be after you set the data property? – Jan Weinkauff Feb 04 '16 at 17:01
  • You might be running into similar issues to this: http://stackoverflow.com/questions/25263882/use-a-generic-class-as-a-custom-view-in-interface-builder – GetSwifty Oct 04 '16 at 18:42

0 Answers0