开发者

Creating an NSTimer with varying interval through a wrapper method not working

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 differ开发者_运维知识库ent 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'.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜