有一个要求,我需要从其他类调用方法,但是方法名称会随着不同的动作而改变,如下所示:
NSArray *arrays = [NSArray arrayWithObjects:@"aaa", @"bbb", nil];
for ( NSString *array in arrays ){
blablabla.......
Aclass *abc = [[Aclass alloc] methodName%@:variable],array;
blablabla.......
}
有没有办法在 Objective-C 中编写类似的代码?
谢谢!
更多信息:Aclass 中的方法名称如下:
-(NSArray *)procWithAAA:(NSInteger)aaaID
谢谢!
Aclass 中的方法名称如下:
-(NSArray *)procWithAAA:(NSInteger)aaaID
---------根据你们的建议,我编写如下代码,但它不起作用--------
NSArray *arrays = [NSArray arrayWithObjects:@"aaa", @"bbb", nil];
for ( NSString *array in arrays ){
SEL customSelector = NSSelectorFromString([NSString stringWithFormat:@"procWith%@", array]);
if ([Sync respondsToSelector:customSelector]) {
Aclass * abc = [Aclass performSelector:customSelector:aaaID];
}else {
NSLog(@"## Class does not respond to %@", customSelector);
}
Aclass * abc = [Aclass performSelector:customSelector:aaaID]; 这部分在 Objective-C 中是不允许的。
谢谢