I’m used to using eval() in languages like javascript e.t.c.
In cocos2d, to select the level, I am passing “1” or “2” to my Loadlevel.m file, the level Classes are named “LevelOne” and “LevelTwo” accordingly, I wanted to create a dictionary lookup that paried “1” => “LevelOne” e.t.c then run eval on that string to effectively call [MyLevel node];
Apparently we can’t use eval in IOS code, so how would I go about doing this?
Try using the
NSStringFromClassandNSClassFromStringfunctions.Specifically, represent the classes as strings in your dictionary, e.g.,
Then, to use the right level from your dictionary, you could do:
(I’m assuming
+nodeis a class method)Now, this is a pretty uncommon and odd design in Cocoa. Perhaps if you explain more about what you’re doing, we could suggest alternate design choices that might be better.