Load a class from String

I often need to dynamically load a class from a concatinated combination of strings and integers. So here is how you do it. In a game for example, you may have a LevelID stored in your appdelegate that changes at the game progresses, so I’ve made the example grabbing such a levelID from the delegate class.

YourAppDelegate *theDelegate = (YourAppDelegate*)[[UIApplication sharedApplication] delegate];
int LevelID = [theDelegate getLevelID];
NSString *levelToLoad = [NSString stringWithFormat:@"Level%i",LevelID];
Class newLevel = NSClassFromString(levelToLoad);

BAM, and it’s done.