gpt4 book ai didi

objective-c - objective C 使用字符串调用方法

转载 作者:太空狗 更新时间:2023-10-30 03:47:35 26 4
gpt4 key购买 nike

嗨,我是 Objective-C 的新手,想知道是否有人可以帮助我解决这个问题。我有几个不同的方法,每个方法都需要 3 个输入值,通常使用

[self methodA:1 height:10 speed:3]

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

[self methodB:1 height:10 speed:3] 

对于“方法C”

[self methodC:1 height:10 speed:3]

等等。

关于如何执行此操作的任何想法,我尝试使用 NSSelectorFromString 将字符串定义为选择器

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

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

所以为了回顾一下,我有一些方法,例如

 spawnEnemyA:2 withHeight:3 withSpeed:4  
spawnEnemyB:3 withHeight:2 withSpeed:5

我想从 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 objectForKey:@"enemytype"];
NSString *label1 = @"spawn";
NSString *label2 = @":withHeight:withSpeed:";
NSString *combined = [NSString stringWithFormat:@"%@%@%@",label1, method,label2];


//Check that the string is correct get spawnEnemyA:withHeight:withSpeed:
CCLOG(@"%@",combined);


//This is the Invocation part
NSInvocation * invocation = [ NSInvocation new ];

[ invocation setSelector: NSSelectorFromString(combined)];
[ invocation setArgument: &c atIndex: 2 ];
[ invocation setArgument: &b atIndex: 3 ];
[ invocation setArgument: &a atIndex: 4 ];

[ invocation invokeWithTarget:self ];

[invocation release ];

/////////////////////////////////////////////////////////////////

代码编译没有任何错误,但没有调用方法。有任何想法吗?干杯

最佳答案

您不能将 performSelector 用于具有 3 个(或更多)参数的方法。
但为了您的信息,这里是如何使用它:

SEL m1;
SEL m2;
SEL m3;

m1 = NSSelectorFromString( @"someMethodWithoutArg" );
m2 = NSSelectorFromString( @"someMethodWithAnArg:" );
m1 = NSSelectorFromString( @"someMethodWithAnArg:andAnotherOne:" );

[ someObject performSelector: m1 ];
[ someObject performSelector: m2 withObject: anArg ];
[ someObject performSelector: m2 withObject: anArg withObject: anOtherArg ];

对于超过 2 个参数的方法,您必须使用 NSInvocation
查看文档以了解如何使用它。

基本上:

NSInvocation * invocation = [ NSInvocation new ];

[ invocation setSelector: NSStringFromSelector( @"methodWithArg1:arg2:arg3:" ) ];

// Argument 1 is at index 2, as there is self and _cmd before
[ invocation setArgument: &arg1 atIndex: 2 ];
[ invocation setArgument: &arg2 atIndex: 3 ];
[ invocation setArgument: &arg3 atIndex: 4 ];

[ invocation invokeWithTarget: targetObject ];

// If you need to get the return value
[ invocation getReturnValue: &someVar ];

[ invocation release ];

关于objective-c - objective C 使用字符串调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11125640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com