Performselector - 参数 2 从整数生成指针而不进行强制转换

发布于 2024-11-27 06:12:44 字数 506 浏览 2 评论 0原文

我有这样的代码:

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

对于这个方法:

-(void)animationWithType:(PasscodeAnimationType)type;

将它放在它的位置:

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

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

I have this code:

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

To this method:

-(void)animationWithType:(PasscodeAnimationType)type;

Putting this in it's place:

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

returns an NSLog of "1", which my method doesn't class as the same value as PasscodeAnimationTypeConfirm. How can i fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻之岛 2024-12-04 06:12:44

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

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

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

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

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

Yeah, as far as I know, you can only do performSelector:blahBlah:withDelay: with objects as parameters, not types (ints, chars, etc.).

You can create another function to help you, like this:

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

And using this one from the code you posted:

[self performSelector:@selector(animationWithType:) withObject:[NSNumber numberWithInt:PasscodeAnimationTypeConfirm] afterDelay:0.2];
凉城凉梦凉人心 2024-12-04 06:12:44

@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];
    }
}

@Emilio: Shouldn't your performSelector call invoke your new method?

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

Here is an example of mine showing a more detailed use of this concept.

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

The adapter looks like this and performs some more checks:

-(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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文