NSClassFromString Case Insensitive Objective-C
I am attempting to instantiate classes from different XML sources. The cases of the class names are inconsistent (camel, upper, lower) across these sources. Does an equivalent to NSClassFromString exist for this? For example, something allowing for:
Person *person = [[NSClassFromCaseInsensitiveString("Person") alloc] init];
Person *person = [[NSClassFromCaseInsensitiveString("person") alloc] init];
Person *person = [[NSClassFromCaseInsensitiveString("PERSON") alloc] i开发者_如何学Gonit];
make a NSString case insensitive by using any of the capitalization methods in NSString (-capitalizedString
for instance) then use it as the param for NSClassFromString
Edit: here is code based on your Person example. (Assuming stringFromXML is a NSString that you parsed from your XML code that could contain any form of the word "person"
NSString *personString = stringFromXML;
Person *person = NSClassFromString([personString capitalizedString]);
You're probably stuck with searching the class list, obtainable from objc_getClassList
Please don't do this. You're letting arbitrary (I'm assuming downloaded-over-HTTP) XML instantiate arbitrary classes in your app. This is bad, and it's difficult to know how bad because it can instantiate any class loaded in your app (including ones in private frameworks).
Consider using something like
NSDictionary * classesByLowercaseString = [NSDictionary dictionaryWithObjectsAndKeys:
[Person class], @"person",
[Blah class[, @"blah",
nil];
[classesByLowercaseString objectForKey:[xmlClassName lowercaseString]];
Also note that XML tag names are supposed to be case-sensitive.
You could use the NSString capitalization methods, if you are guaranteed that the class name will look like that. A general function as you describe is not available, and likely won't be, for the simple reason that class names are not case insensitive.
@interface Person : NSObject
{
}
@end
@interface PERSON : NSObject
{
}
@end
will work and declare two different class types. Though in all honesty, having classes with identical names except case would just be bad style in the first place.
精彩评论