I have a global dictionary with Class objects and NSString keys. Every custom subclass of my Property class can register itself on the Property superclass using
+ (void)registerPropertyClass:(Class)pclass forNamePrefix:(NSString *)namePrefix
A Property is initialized with a name and value. What I want to do now is return a different class from the init method based on the name prefix (if a registered class matches).
Would something like this be correct?
- (id)initWithName:(NSString *)name value:(NSString *)value
id instance = self;
NSArray *registeredPrefixes = [kCKPropertyClasses allKeys];
for (NSString *prefix in registeredPrefixes) {
if ([name rangeOfString:prefix].location == 0) {
instance = [[kCKPropertyClasses objectForKey:prefix] alloc];
break;
}
}
self = [instance init];
if (self) {
self.name = name;
self.value = value;
}
return self;
}
UPDATE: Forgot to mention that this project is using ARC (so no retain/release)
Without compiling it, that’ll work, but leak under manual-retain-release. You need to
releaseselfprior to the reassignmentself = [instance init];.With ARC, it should be fine.