通过包装方法创建具有不同间隔的 NSTimer 不起作用
我正在尝试使用包装器方法创建一个 NSTimer,以便我可以使其无效并将其替换为具有不同间隔的新 NSTimer。这是我的代码:
- (void) createTimerWithInterval:(float *)interval{
self.timer = [NSTimer scheduledTimerWithTimeInterval:[[NSNumber numberWithFloat: interval]floatValue] target:self selector:@selector(scrollWrapperForTimer) userInfo:nil repeats:YES];
}
我从编译器得到以下结果。为什么?
“numberWithFloat”的参数 1 的类型不兼容。
I'm trying to create an NSTimer using a wrapper method, so that I can invalidate it and replace it with a new NSTimer with a different interval. Here is my code:
- (void) createTimerWithInterval:(float *)interval{
self.timer = [NSTimer scheduledTimerWithTimeInterval:[[NSNumber numberWithFloat: interval]floatValue] target:self selector:@selector(scrollWrapperForTimer) userInfo:nil repeats:YES];
}
I am getting the following result from the compiler. Why?
incompatible type for argument 1 of 'numberWithFloat'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你应该按值传递间隔,而不是按引用:
但是你的代码也可以这样简化:
不要太苛刻,但是你的代码很尴尬,表明你错过了 Objective-C 中某个地方的概念。你最好弄清楚它是什么并把它捡起来。
You should be passing interval in by value, not by reference:
But your code can also be simplified thus:
Not to be harsh, but your code is awkward enough to indicate you've missed a concept somewhere in Objective-C. You'd be wise to figure out what it is and pick it up.
您确定要传递一个指向浮点数的指针吗?
Are you sure you meant to pass a pointer to a float?
numberWithFloat:
接受一个浮点参数,但您向它传递一个指向浮点的指针。您可以将 *interval 传递给 numberWithFloat: 以传递浮点数的值而不是指向该值的指针。
numberWithFloat:
takes a float argument, but you're passing it a pointer to a float.You could instead pass *interval to numberWithFloat: to pass the value of the float instead of a pointer to the value.