小编tom*_*omo的帖子

objective C使用字符串来调用方法

嗨,我是目标C的新手,想知道是否有人可以帮我这个.我有几个不同的方法,每个方法需要3个输入值,通常使用它来调用它

[self methodA:1 height:10 speed:3]
Run Code Online (Sandbox Code Playgroud)

但是我想从plist中的字符串中读取方法名称,例如,如果字符串是methodB我会得到

[self methodB:1 height:10 speed:3] 
Run Code Online (Sandbox Code Playgroud)

为"methodC"

[self methodC:1 height:10 speed:3]
Run Code Online (Sandbox Code Playgroud)

等等.

任何想法如何我这样做我尝试使用NSSelectorFromString将字符串定义为选择器

NSString *string = [plistA objectForKey:@"method"];
SEL select = NSSelectorFromString(string);
[self performSelector:select:c height:b speed:a]; 
Run Code Online (Sandbox Code Playgroud)

然而,这不起作用任何帮助将不胜感激.尝试了下面的解决方案,但无法在这里工作是我尝试过的.

所以我只想回顾一下我的方法

 spawnEnemyA:2 withHeight:3 withSpeed:4  
 spawnEnemyB:3 withHeight:2 withSpeed:5 
Run Code Online (Sandbox Code Playgroud)

我想读取我想传递给这些方法的值以及plist文件中的方法类型.我的代码如下,//////////////////////////////////////////// //////////////////

//这些是我从plist中读取的值,我希望我的方法可以使用

    int a = [[enemySettings objectForKey:@"speed"] intValue];
    int b = [[enemySettings objectForKey:@"position"] intValue];
    int c = [[enemySettings objectForKey:@"delay"] intValue];

   // I Also read the method name from the plist and combine it into a single string  
    NSString *method = [enemySettings …
Run Code Online (Sandbox Code Playgroud)

string methods objective-c plist

5
推荐指数
2
解决办法
5552
查看次数

标签 统计

methods ×1

objective-c ×1

plist ×1

string ×1