I am working on an object factory to keep track of a small collection of objects. The objects can be of different types, but they will all respond to createInstance
and reset
. The objects can not be derived from a common base class because some of them will have to derive from built-in cocoa classes like NSView
and NSWindowController
.
I would like to be able to create instances of any suitable object by simply passing the desired classname to my factory as follows:
myClass * variable = [factory makeObjectOfClass:myClass];
The makeObjectOfClass:
method would look something like this:
- (id)makeObjectOfClass:(CLASSNAME)className
{
assert([className instancesRespondToSelector:@selector(reset)]);
id newInstance = [className createInstance];
[managedObjects addObject:newInstance];
return newInstance;
}
Is there a way to pass a class name to a method, as I have done with the (CLASSNAME)className
argument to makeObjectOfClass:
above?
For the sake of completeness, here is why I want to manage all of the objects. I want to be able to reset the complete set of objects in one shot, by calling [factory reset];
.
- (void)reset
{
[managedObjects makeObjectsPerformSelector:@selector(reset)];
}