4

我试图用 NSTimeInterval 倒计时。但我希望能够更改间隔而无需每次都发布更新。所以我尝试从我的网站导入 Timeinterval。我已经将 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 没有做任何事情......

4

3 回答 3

7

您的网址http://gymnasium2.ai.ch/~mensa/countdown_timestamp.html是一个完整的 HTML 文件。 NSString initWithContentsOfURL:将返回一个包含其所有内容的字符串:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>1328633219</title>
</head>

<body>
1328633219
</body>
</html>

这不能转换为双精度;您需要解析 HTML,这可能是很多工作。

放置一个仅包含数字的简单文件会更容易:

1328633219

然后您上面的代码将能够在不进行任何更改的情况下获得该号码。

但是,以下代码可能会阻止您的代码工作:

NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://gymnasium2.ai.ch/~mensa/countdown_timestamp.html"]];
label.text = string; // This line
double timeInterval = [label.text doubleValue];

如果labelnil,则timeInterval不会正确设置,因为[label.text doubleValue]也会返回nil。您可以尝试:

NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://gymnasium2.ai.ch/~mensa/countdown_timestamp.html"]];
double timeInterval = string;
label.text = [NSString stringWithFormat:@"%d", (int)timeInterval];

在获取文件后删除断点或添加 NSLog 调用会很有帮助,这样您就可以看到发生了什么。

NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://gymnasium2.ai.ch/~mensa/countdown_timestamp.html"]];
NSLog(@"Downloaded file with contents: %@", string);

或者,您可以上传plist文件并使用类似NSDictionary dictionaryWithContentsOfURL:NSArray arrayWithContentsOfURL:. 请参阅属性列表编程指南

于 2012-02-07T18:02:40.483 回答
2
NSTimeInterval timeInterval = [self timeIntervalFromString:@"00:01:40"];
NSLog(@"timeInterval %f", timeInterval);

- (NSTimeInterval)timeIntervalFromString:(NSString *)string {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSDate *start = [dateFormatter dateFromString:@"00:00:00"];
NSDate *end = [dateFormatter dateFromString:string];
NSTimeInterval interval = [end timeIntervalSinceDate:start];
return interval;
}

输出:时间间隔 100.000000

于 2013-08-21T02:18:51.433 回答
1

您是否完全通过代码(使用 Xcode 调试器)查看代码的哪一行得到了意外的结果?您从不使用destinationDate,实际上您应该传递给最后一行代码的是:

timer = [NSTimer scheduledTimerWithTimeInterval:intervalForTimer target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];

此外,您从中获取时间间隔值的地方是一个带有 HTML 标记的网页。更改countdown_timestamp.htmlcountdown_timestamp.txt并将您的时间间隔值本身(例如“ 2.0”)放入其中。不要将其包装在 HTML 中。

于 2012-02-07T17:58:19.863 回答