对象NSTimeInterval的调用方法失败

时间:2013-07-07 23:55:36

标签: ios objective-c methods selector nstimeinterval

我正在开发一个应用程序,我会在其中调用方法,并传入参数NSTimeInterval。 我的方法声明如下:

-(void)MethodwithTime:(NSTimeInterval)t
{
    NSLog(@"Test, T = %f", t);
    ...
}

我从UIViewController这样称呼它:

[myObject performSelector:@selector(MethodwithTime:) withObject:[NSNumber numberWithDouble:t]];

因为我读到NSTimeInterval是双倍的。但是NSLog测试给了我一些零...测试,T = 0.000000

我该如何解决?

谢谢!

1 个答案:

答案 0 :(得分:3)

NSTimeInterval不是一个对象(它是某个浮点数类型的typedef,但如果您已阅读其文档,则可能已经知道)。因此,当您尝试将指针(由哪些Objective-C对象表示)解释为浮点基元时,您将获得意外结果。如何将MethodwithTime:的参数类型更改为NSNumber *

(哦,方法名称以小写字母开头并使用camelCaps,因此MethodwithName:不是惯用的,MethodWithName:也不好,methodWithName:是。)