使用 NSTimer 从服务器刷新数据
Refreshing data from server using NSTimer
我想从服务器刷新数据,但是那个刷新时间-> 我想从用户那里得到它并传递给计时器,我该怎么做我知道要给出这样的值,
self.timerCounter =[NSTimer scheduledTimerWithTimeInterval:300.0
target:self selector:@selector(getGraphData) userInfo:nil repeats:YES];
我也尝试过使用协议和代表,
self.timerCounter =[NSTimer scheduledTimerWithTimeInterval:strTime
target:self selector:@selector(getGraphData) userInfo:nil repeats:YES];
但我遇到了类似
的错误
Sending NSString _strong to parameter of incompatible type
NSTimerInterval
现在,我想从文本字段中的用户那里得到它,请帮助我做到这一点
将 textfield.text
转换为 NSTimeInterval
试试看,
let strTime : NSTimeInterval = NSTimeInterval(textfield.text)!
您正在使用 NSString
而不是 NSTimeInterval
类型的数据。您应该能够通过将 txtTime
或 strTime
转换为双精度值来解决此问题。
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *url = [defaults objectForKey:@"server_url"];
NSString *txtTime = [defaults objectForKey:@"refresh_time"];
if (txtTime != nil) {
NSTimeInterval timeInterval = [txtTime doubleValue];
self.timerCounter = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(getGraphData) userInfo:nil repeats:true];
}
我认为将 strTime
转换为 NSTimeInterval 时存在问题。
self.timerCounter =[NSTimer scheduledTimerWithTimeInterval:[strTime doubleValue] target:self selector:@selector(getGraphData) userInfo:nil repeats:YES];
试试这个,如果有转换问题,这应该可以工作..
我想从服务器刷新数据,但是那个刷新时间-> 我想从用户那里得到它并传递给计时器,我该怎么做我知道要给出这样的值,
self.timerCounter =[NSTimer scheduledTimerWithTimeInterval:300.0
target:self selector:@selector(getGraphData) userInfo:nil repeats:YES];
我也尝试过使用协议和代表,
self.timerCounter =[NSTimer scheduledTimerWithTimeInterval:strTime
target:self selector:@selector(getGraphData) userInfo:nil repeats:YES];
但我遇到了类似
的错误Sending NSString _strong to parameter of incompatible type NSTimerInterval
现在,我想从文本字段中的用户那里得到它,请帮助我做到这一点
将 textfield.text
转换为 NSTimeInterval
试试看,
let strTime : NSTimeInterval = NSTimeInterval(textfield.text)!
您正在使用 NSString
而不是 NSTimeInterval
类型的数据。您应该能够通过将 txtTime
或 strTime
转换为双精度值来解决此问题。
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *url = [defaults objectForKey:@"server_url"];
NSString *txtTime = [defaults objectForKey:@"refresh_time"];
if (txtTime != nil) {
NSTimeInterval timeInterval = [txtTime doubleValue];
self.timerCounter = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(getGraphData) userInfo:nil repeats:true];
}
我认为将 strTime
转换为 NSTimeInterval 时存在问题。
self.timerCounter =[NSTimer scheduledTimerWithTimeInterval:[strTime doubleValue] target:self selector:@selector(getGraphData) userInfo:nil repeats:YES];
试试这个,如果有转换问题,这应该可以工作..