将 NSString 转换为 NSTimeInterval
我尝试使用 NSTimeInterval 进行倒计时。但我希望能够更改间隔,而不必每次都发布更新。所以我尝试从我的网站导入时间间隔。我已将 NSTimeInterval 的数字存储在 NSString 中,现在希望将它们转换为 NSTimeInterval 以便将其实现到倒计时代码中......
但它不起作用。有什么想法吗?
label.text = string;
double timeInterval = [label.text doubleValue];
NSTimeInterval intervalForTimer = timeInterval;
destinationDate = [[NSDate dateWithTimeIntervalSince1970:intervalForTimer] retain];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
编辑:
- (void)updateLabel {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[dateLabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c %d%c", [components month], 'm', [components day], 'd', [components minute], 'm', [components second], 's']];
}
我的新代码看起来像这样:
NSLog(@"Downloaded file with contents: %@", string);
double timeInterval = [string doubleValue];
label.text = [NSString stringWithFormat:@"%d", (int)timeInterval];
NSTimeInterval intervalForTimer = timeInterval;
destinationDate = [[NSDate dateWithTimeIntervalSince1970:timeInterval] retain];
timer = [NSTimer scheduledTimerWithTimeInterval:intervalForTimer target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
但是 dateLabel 没有做任何事情......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的网址,http://gymnasium2.ai.ch/~mensa/countdown_timestamp.html,是一个完整的 HTML 文件。
NSString initWithContentsOfURL:
将返回一个包含其所有内容的字符串:这不能转换为双精度;您需要解析 HTML,这可能是一项相当大量的工作。
放置一个仅包含数字的简单文件会更容易:
然后上面的代码将能够在不进行任何更改的情况下获取数字。
但是,以下代码可能会阻止您的代码运行:
如果
label
为nil
,则timeInterval
将无法正确设置,因为[label.text doubleValue]
也将返回nil
。您可以尝试:删除断点或在获取文件后添加 NSLog 调用会很有帮助,这样您就可以看到发生了什么。
或者,您可以上传
plist
文件并使用NSDictionary DictionaryWithContentsOfURL:
或NSArray arrayWithContentsOfURL:
等内容。请参阅属性列表编程指南。Your URL, http://gymnasium2.ai.ch/~mensa/countdown_timestamp.html, is a full HTML file.
NSString initWithContentsOfURL:
will return a string containing all of its content:This can't be converted to a double; you'd need to parse the HTML, which can be quite a lot of work.
It would be easier to put a simple file up containing only the number:
Then your code above would be able to get the number without any changes.
However, the following code might prevent your code from working:
If
label
isnil
, thentimeInterval
won't be set properly, because[label.text doubleValue]
will also returnnil
. You might try instead:It would be helpful to drop a breakpoint, or add an NSLog call after you fetch the file, so you can see what's going on.
Alternatively, you could upload a
plist
file and use something likeNSDictionary dictionaryWithContentsOfURL:
orNSArray arrayWithContentsOfURL:
. See the property list programming guide.输出:时间间隔 100.000000
output: timeInterval 100.000000
您是否单步执行代码(使用 Xcode 调试器)来查看哪一行代码得到了意外结果?您从不使用
destinationDate
,实际上您应该传递到最后一行代码的是:timer = [NSTimerchedTimerWithTimeInterval:intervalForTimer target:selfselector:@selector(updateLabel)userInfo: nil Repeats:YES];
另外,您获取时间间隔值的位置是一个周围带有 HTML 标记的网页。将
countdown_timestamp.html
更改为countdown_timestamp.txt
,然后将时间间隔值本身(例如“2.0
”)放入其中。不要将其包装在 HTML 中。did you step through the code at all (using the Xcode debugger) to see which line of your code was getting unexpected results? You never use
destinationDate
, and in fact what you should be passing to the last line of code is:timer = [NSTimer scheduledTimerWithTimeInterval:intervalForTimer target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
Also, the place where you're getting your time interval value from is a web page with HTML tags all around it. Change
countdown_timestamp.html
tocountdown_timestamp.txt
and just put your time interval value, by itself (e.g. "2.0
") into it. Don't wrap it in HTML.