How can I pass a class name as an argument to an object factory in cocoa?
You can convert a string to a class using the function: NSClassFromString
Class classFromString = NSClassFromString(@"MyClass");
In your case though, you'd be better off using the Class objects directly.
MyClass * variable = [factory makeObjectOfClass:[MyClass class]];- (id)makeObjectOfClass:(Class)aClass{ assert([aClass instancesRespondToSelector:@selector(reset)]); id newInstance = [aClass createInstance]; [managedObjects addObject:newInstance]; return newInstance;}
I have right a better tutorial on that , please checkouthttps://appengineer.in/2014/03/13/send-class-name-as-a-argument-in-ios/
It's pretty easy to dynamically specify a class, in fact you can just reference it by it's name:
id string = [[NSClassFromString(@"NSString") alloc] initWithString:@"Hello!"];NSLog( @"%@", string );
One other tip, I would avoid using the nomenclature 'managed object' since most other Cocoa programmers will read that as NSManagedObject, from Core Data. You may also find it easier to use a global NSNotification (that all your reset-able objects subscribe to) instead of managing a collection of different types of objects, but you're more informed to make that decision than I am.