Performselector - 参数2使得指针来自整数而不进行强制转换

时间:2011-07-30 14:44:20

标签: iphone objective-c performselector

我有这段代码:

[self performSelector:@selector(animationWithType:) withObject:PasscodeAnimationTypeConfirm afterDelay:0.2];

采用这种方法:

-(void)animationWithType:(PasscodeAnimationType)type;

把它放在它的位置:

[self performSelector:@selector(animationWithType:) withObject:[NSNumber numberWithInt:PasscodeAnimationTypeConfirm] afterDelay:0.2];

返回NSLog为“1”,我的方法不会将其作为与PasscodeAnimationTypeConfirm相同的值。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

是的,据我所知,你只能执行performSelector:blahBlah:withDelay:将对象作为参数,而不是类型(整数,字符等)。

您可以创建另一个功能来帮助您,如下所示:

-(void)animationWithNumber:(NSNumber)type{
    [self animationWithType:[NSNumber intValue]];
}

使用您发布的代码中的这个:

[self performSelector:@selector(animationWithType:) withObject:[NSNumber numberWithInt:PasscodeAnimationTypeConfirm] afterDelay:0.2];

答案 1 :(得分:1)

@Emilio:你的performSelector调用不应该调用你的新方法吗?

[self performSelector:@selector(animationWithNumber:) withObject:[NSNumber numberWithInt:PasscodeAnimationTypeConfirm] afterDelay:0.2];

以下是我的一个例子,展示了这个概念的更详细用法。

http://kerkermeister.net/objective-c-adapter-from-nsinteger-to-id-when-using-performselector-withobject/

适配器看起来像这样并执行更多检查:

-(void)animationWithNumber:(id)_animationType {
    if ([_animationType respondsToSelector:@selector(intValue)]) {
        int _t = [_animationType intValue];
        switch (_t) {
            case AnimationType1:
                _t = AnimationType1;
                break;
            case AnimationType2:
                _t = AnimationType2;
                break;
            default:
                _t = AnimationTypeUndefined;
                break;
        }
        [self animationWithType:_t];
    }
}