In Objective-C there is the Alloc/Init metaphor. They've also added a shared convenience method called 'new' that internally just calls both in succession. And if I create a subclass of NSObject called FooClass, FooClass picks up those shared methods, including 'new'.
BUT... how the heck is that implemented??
It can't simply delegate to the base class because that would just instantiate an instance of NSObject, not your derived class FooClass, yet it still works! So how would someone write something similar?
In other words, the base class shouldn't be this...
+ (id) somethingLikeNew{
return [[NSObject alloc] init];
}
But rather this...
+ (id) somethingLikeNew{
return [[<SomethingThatMapsToFooClassType> alloc] init];
}
...where 'SomethingThatMapsToFooClassType' is the type of the derived class that inherits from NSObject and which needs to pick up the shared method 'somethingLikeNew'.
Basically I'm adding a category off of NSObject and I have shared methods that need to know the type, but the implementations are all generic, hence going in a category on NSObject and not all over the place in my class files (the same way you don't have 'new' all over the place. It's just there.)
Anyone? Bueller? Bueller?
M