通过包装方法创建具有不同间隔的 NSTimer 不起作用

发布于 2024-09-30 20:17:00 字数 433 浏览 2 评论 0原文

我正在尝试使用包装器方法创建一个 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 技术交流群。

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

发布评论

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

评论(3

农村范ル 2024-10-07 20:17:00

你应该按值传递间隔,而不是按引用:

- (void) createTimerWithInterval:(float)interval{

但是你的代码也可以这样简化:

- (void) createTimerWithInterval:(float)interval{

    self.timer = [NSTimer scheduledTimerWithTimeInterval:interval
                                                  target:self
                                                selector:@selector(scrollWrapperForTimer)
                                                userInfo:nil
                                                 repeats:YES];
}

不要太苛刻,但是你的代码很尴尬,表明你错过了 Objective-C 中某个地方的概念。你最好弄清楚它是什么并把它捡起来。

You should be passing interval in by value, not by reference:

- (void) createTimerWithInterval:(float)interval{

But your code can also be simplified thus:

- (void) createTimerWithInterval:(float)interval{

    self.timer = [NSTimer scheduledTimerWithTimeInterval:interval
                                                  target:self
                                                selector:@selector(scrollWrapperForTimer)
                                                userInfo:nil
                                                 repeats:YES];
}

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.

逆光下的微笑 2024-10-07 20:17:00

您确定要传递一个指向浮点数的指针吗?

Are you sure you meant to pass a pointer to a float?

寒冷纷飞旳雪 2024-10-07 20:17:00

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.

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