views:

250

answers:

1

I'm going to create a base class that implements very similar functions for all of the subclasses. This was answered in a different question. But what I need to know now is if/how I can cast various functions (in the base class) to return the subclass object. This is both for a given function but also a function call in it.

(I'm working with CoreData by the way)

As a function within the base class (this is from a class that is going to become my subclass)

+(Structure *)fetchStructureByID:(NSNumber *)structureID inContext:(NSManagedObjectContext *)managedObjectContext {...}  

And as a function call within a given function:

Structure *newStructure = [Structure fetchStructureByID:[currentDictionary objectForKey:@"myId"]];
                                              inContext:managedObjectContext];

Structure is one of my subclasses, so I need to rewrite both of these so that they are "generic" and can be applied to other subclasses (whoever is calling the function).

How do I do that?

Update: I just realized that in the second part there are actually two issues. You can't change [Structure fetch...] to [self fetch...] because it is a class method, not an instance method. How do I get around that too?

+1  A: 

If I understand your question correctly I believe the key is the [self class] idiom.

As far as your update goes requesting a way to call a class method on the current class you can use [self class]. As in:

Structure *newStructure = [[self class] fetchStructureByID:[currentDictionary 
                                              objectForKey:@"myId"]];
                                                 inContext:managedObjectContext];

EDIT: I redid this to return id per @rpetrich's comment -- much cleaner and avoids the need for -isKindOfClass: as long as you're sure of the type of the instance you're calling -createConfiguredObject on.

As for the first part, you could just return an id (pointer to any object) and document that it will return an instance of the same class it's called upon. Then in the code you need to use [self class] anywhere you instantiate a new object in a method.

e.g. if you have a -createConfiguredObject method which returns an instance of the same class it's called on, it would be implemented as follows:

// Returns an instance of the same class as the instance it was called on.
// This is true even if the method was declared in a base class.
-(id) createConfiguredObject {
    Structure *newObject = [[[self class] alloc] init];
    // When this method is called on a subclass newObject is actually
    // an instance of that subclass
    // Configure newObject
    return newObject;
}

You can then use this in code as follows:

StructureSubclass *subclass = [[[StructureSubclass alloc] init] autorelease];
subclass.name = @"subclass";

// No need to cast or use isKindOfClass: here because returned object is of type id
// and documented to return instance of the same type.
StructureSubclass *configuredSubclass = [[subclass createConfiguredObject] autorelease];
configuredSubclass.name = @"configuredSubclass";

For reference, what I was referring to with -isKindOfClass: and casting to the proper subclass is as follows:

Structure *structure;
// Do stuff
// I believe structure is now pointing to an object of type StructureSubclass
// and I want to call a method only present on StructureSubclass.
if ([structure isKindOfClass:[StrucutreSubclass class]]) {
    // It is indeed of type StructureSubclass (or a subclass of same)
    // so cast the pointer to StructureSubclass *
    StructureSubclass *subclass = (StructureSubclass *)structure;
    // the name property is only available on StructureSubclass.
    subclass.name = @"myname";
} else {
    NSLog(@"structure was not an instance of StructureSubclass when it was expected it would be.");
    // Handle error
}
Lawrence Johnston
What do you mean by "before casting to the proper subclass"? This has helped me a lot so far, there are still a few aspects I'm trying to work out. Especially when trying to set a variable like newObject.name = ... since Structure (in your case the base class) doesn't have name, but the subclass(es) do. In that case do I just need to make Structure look like it has those variables too?
RyanJM
Please see my edit (and upvote/accept if my answer is useful to you).
Lawrence Johnston
Sorry for the late response. It took me awhile to implement it and all. This really helped a lot, thank you.
RyanJM
You're welcome. I'm glad it was helpful.
Lawrence Johnston