NSClassFromString Case-insensitive Objective-C

I am trying to instantiate classes from different XML sources. Cases of class names are inconsistent (camel, upper, lower) in these sources. Is there an equivalent to NSClassFromString for this? For example, something that allows you to:

Person *person = [[NSClassFromCaseInsensitiveString("Person") alloc] init];
Person *person = [[NSClassFromCaseInsensitiveString("person") alloc] init];
Person *person = [[NSClassFromCaseInsensitiveString("PERSON") alloc] init];
+3
source share
4 answers

You are probably stuck looking for a list of classes that you can get from objc_getClassList

+3
source

make the NSString case insensitive using any of the capitalization methods in NSString (for example -capitalizedString), then use it as a parameter for NSClassFromString

: , Person. (, stringFromXML NSString, XML-, ""

NSString *personString = stringFromXML;
Person *person = NSClassFromString([personString capitalizedString]);
+6

, . ( , HTTP) XML . , , , , ( ).

, -

NSDictionary * classesByLowercaseString = [NSDictionary dictionaryWithObjectsAndKeys:
  [Person class], @"person",
  [Blah class[, @"blah",
  nil];
[classesByLowercaseString objectForKey:[xmlClassName lowercaseString]];

, XML .

+3

NSString, , . , , , , , , .

@interface Person : NSObject  
{ 

} 

@end 

@interface PERSON : NSObject
{

}

@end

will work and declare two different types of classes. Although, frankly, the presence of classes with the same name, except in cases, would be just a bad style in the first place.

+1
source

Source: https://habr.com/ru/post/1759565/


All Articles