4

First things first... this has been voted to close as a duplicate of why you can't use Generics directly in Interface Builder. The TLDR answer there is IB uses Objective-C and Objective-C doesn't support generics. That and there's no way to specify a generic's 'specialty' (i.e. what type it uses) anyway. That's understood and makes complete sense.

This question however is about why you can't use a subclass of a fully-defined/specified generic, which again, fully defines that generic's specialty so IB doesn't need to know anything about generics or even that a generic is being used in the first place.

Consider the following class...

class MyView : UIView
{
}

We can easily use this class in Interface Builder by dragging out a view, then changing its class type to be MyView. When instantiated, MyViews init-with-coder is called as expected.

Now consider these classes...

class MyGenericView<T> : UIView
{
}

class MyView : MyGenericView<String>
{
}

In this case, even though MyView is based on the concrete type MyGenericView<String> which itself is based on UIView, you can't use this class in Interface Builder. But why? This is a fully-qualified class that is a subclass of UIView, but the application warns that it cannot find the specified class.

Of note, and to clarify my question, it's my understanding that MyView here is not a Generic. It's a fully-defined concrete type that can be instantiated directly from its class-name and is ultimately based on NSObject (via UIView) so it would seem it should be fully compatible with Objective-C/IB, but that doesn't appear to be the case. I'm trying to understand why that is because it's my understanding that Generics are a compiler feature, not a runtime feature meaning they get compiled right down to non-generic objects, but I could be wrong.

So... is this a bug? If not, can someone shed some light on exactly why this particular scenario doesn't work as expected, addressing the compiler/runtime observations I mentioned from a technical perspective?

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • 2
    Possible duplicate of [Use a generic class as a custom view in Interface Builder](http://stackoverflow.com/questions/25263882/use-a-generic-class-as-a-custom-view-in-interface-builder) – GetSwifty Nov 29 '16 at 00:16

0 Answers0