Thomas
Thomas

Reputation: 123

How to convert a method name into a String

I´m developing a little game right now and I want to have several levels. So i´m asking if it´s the first time the app gets launched. If it´s like that the user will start at level1. But if now i want to start another method. I have methods like:

-(void)level1{};
-(void)level2{};

. . . till level 100 or so. So after if got the amount of times the app got started i want to call a method, which fits. So i tried to make it like:

[self [NSString stringWithString:@"level%d", level]];

Because "level" has a number like 1,2,3...,100. So i tried to make it like level1,level2, level3, and so on. But for that I get an error -> Unexpected interface name 'NSString': expected expression. Can anyone please help me? Thanks in advance! Peace!

Upvotes: 0

Views: 136

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90701

You need to use NSSelectorFromString():

NSString* methodName = [NSString stringWithFormat:@"level%d", level];
SEL sel = NSSelectorFromString(methodName);
[self performSelector:sel];

Upvotes: 1

Related Questions