Question

I think that this may be impossible, but what I was trying to do write an static initializer in my super class "load" that will initialize even my subclasses.

+ (id) load
{
    Class c = [self class];
    NSString *cString = NSStringFromClass([self class]);
    NSLog(@"%@",cString);

    id a = [[c alloc] init];
    [a autorelease];
    return a;
}

The result of [self class] here is the superclass, but I want to initialize the class itself (in this case it will always be the subclass). Maybe this is just a terrible programming idea though.

Was it helpful?

Solution

I must type [[[b alloc] init] release], which to me is a bit unsightly and what it does is not so evident.

I disagree. It is very evident what this code is doing. It is allocating class B (classes should be capitalized by the way), initializing it, and then releasing it.

[b load]

On the other hand, this tells me nothing other than the fact that b is loading. What is loading? Does it alloc and init?

I would like to define load in a, otherwise it is a waste of time to copy and paste load into b,c,d (inheriting from a)

This is unsightly, and what is does is not so evident. Also, classes really shouldn't know anything about their subclasses.

IMHO, your solution is not a very good one. The way to do it would be for the superclass to have a load method (I would prefer a designated initializer with a more descriptive name) that has all common functionality in it. Then, in your subclasses, override your load mthod to call the super implementatation, then add whatever code you need to for that particular subclass.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top