1

There is a requirement, I need to call method from other class, but the method name is changed with different actions, something like below:

NSArray *arrays = [NSArray arrayWithObjects:@"aaa", @"bbb", nil];
for ( NSString *array in arrays ){
blablabla.......
Aclass *abc = [[Aclass alloc] methodName%@:variable],array;
blablabla.......
}

Is there any way to write code like that in Objective-C?

Thanks!

more info: the method name in Aclass is like:

-(NSArray *)procWithAAA:(NSInteger)aaaID

Thanks!

the method name in Aclass is like:

-(NSArray *)procWithAAA:(NSInteger)aaaID

---------as you guys's suggestion, i write code like below, but it doesn't work-------

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]; this part is not allows in objective-C.

Thanks

3 Answers3

1

You can use [self performSelector:<#(SEL)#> withObject:<#(id)#>];

 NSArray *selectorArray = [NSArray arrayWithObjects:@"abc",@"gcd", nil];

for (NSString *method in selectorArray) 
{
     [self performSelector:NSSelectorFromString(method) withObject:nil];
}

Method with parameters,

NSArray *selectorArray = [NSArray arrayWithObjects:@"abc:",@"gcd:", nil];

for (NSString *method in selectorArray) 
{
     [self performSelector:NSSelectorFromString(method) withObject:@"test"];
}
Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • Thanks! the method name in Aclass is like: -(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); } It doesn't work. –  Apr 09 '12 at 06:21
  • Ok.Instead of returning a value you can store in a class level property and access it. I mean you can modify the method to make it return void. I will look for some alternative. – Vignesh Apr 09 '12 at 06:25
  • You should use NSInvocation.http://stackoverflow.com/questions/313400/nsinvocation-for-dummies – Vignesh Apr 09 '12 at 06:26
1

Use NSSelectorFromString to convert a string into a SEL, then -[NSObject performSelector:] or NSInvocation to call the method.

Or, if you can, use Key-Value Coding.

Kurt Revis
  • 27,695
  • 5
  • 68
  • 74
0

You can use the NSSelectorFromString function.

SEL customSelector = NSSelectorFromString(@"selectorName");
if ([class respondsToSelector:customSelector]) {
    [class performSelector:customSelector];
}
else {
    NSLog(@"## Class %@ does not respond to %@", class, customSelector);
}

More information about selectors can be found in the documentation.

Alexander
  • 8,117
  • 1
  • 35
  • 46