0

我在实现某事时遇到了奇怪的编译问题,例如

作品:

  [self performSelector:NSSelectorFromString(member)];

不工作:

  SEL method=NSSelectorFromString(member);
  [self performSelector:method];

编译器哭着:

methods.m:31: error: expected expression before ‘SEL’
methods.m:32: error: ‘method’ undeclared (first use in this function)
methods.m:32: error: (Each undeclared identifier is reported only once
methods.m:32: error: for each function it appears in.)

完整方法:

 - (void)printArgs:(NSString *)member
{   
   //int val=[self compareIt:member];
    switch ([self compareIt:member])
    {
        case 1:
            //dynamically call method
            SEL method;
            method = NSSelectorFromString(member);
            NSLog(@"\n%@ is a object Member , with value: %i\n", member, [self performSelector:method]);            
            break;
        default:
            NSLog(@"\nWrong option :%@ \n", member);
    } 

}
@end

请指出我正确的方向。(请注意我是 Objective-C 的新手)

4

1 回答 1

2

这实际上是一个 C 问题,而不是一个 Objective-C 问题。它不喜欢在 a 中定义变量case而不限制其范围。

这应该有效:

case 1: {
    //dynamically call method
    SEL method;
    method = NSSelectorFromString(member);
    NSLog(@"\n%@ is a object Member , with value: %i\n", member, [self performSelector:method]);
}
break;
于 2012-09-04T12:48:24.610 回答