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?
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:EDIT: I redid this to return
idper @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-createConfiguredObjecton.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
-createConfiguredObjectmethod which returns an instance of the same class it’s called on, it would be implemented as follows:You can then use this in code as follows:
For reference, what I was referring to with
-isKindOfClass:and casting to the proper subclass is as follows: