【问题标题】:Call method with object NSTimeInterval fails使用对象 NSTimeInterval 调用方法失败
【发布时间】:2013-07-05 05:57:01
【问题描述】:

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

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

我从UIViewController这样称呼它:

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

因为我读到NSTimeInterval 是双精度数。但是NSLog 测试给了我一些零……测试,T = 0.000000

我该如何解决?

谢谢!

【问题讨论】:

  • 你为什么使用performSelector:?为什么不直接做:[myObject MethodwithTime:t];
  • 我已经修复它,并要求执行选择器方法 =)

标签: ios objective-c methods selector nstimeinterval


【解决方案1】:

NSTimeInterval 不是对象(它是某些浮点数类型的 typedef,但如果您阅读过它的文档,您可能会知道这一点)。因此,当您尝试将指针(Objective-C 对象由它表示)解释为浮点原语时,您会得到意想不到的结果。把MethodwithTime:的参数类型改成NSNumber *怎么样?

(哦,而且方法名称以小写字母开头并使用驼峰式大写字母,所以MethodwithName: 不是惯用的,MethodWithName: 也不好,methodWithName: 是。)

【讨论】:

  • 如果更改参数是我唯一的解决方案,那么我会这样做。
  • @user2057209 请注意,显然您还必须更改记录方式。
  • @user2057209 请用谷歌搜索。你自己也得努力。
  • 我做到了。这就是为什么我说我已经找到了。请参阅“因为我读到 NSTimeInterval 是双精度”...
  • @user2057209 NSNumber *foo = @(1.5); NSLog(@"%@", foo); 因为它是一个对象。
猜你喜欢
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
  • 2021-12-20
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多