将 NSString 转换为 NSTimeInterval

发布于 2025-01-03 12:58:10 字数 1647 浏览 0 评论 0 原文

我尝试使用 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 没有做任何事情......

I tried to make a countdown with the NSTimeInterval. But I want to be able to change the interval without releasing an update each time. So I tried to import the Timeinterval from my website. I've stored the numbers for the NSTimeInterval in a NSString and want now to convert them into NSTimeInterval in order to implement it into the Countdown code...

...but it's not working. Any ideas?

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];

edit:

- (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']];

}

My new code looks like that:

    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];

But the dateLabel doesn't do anything...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

大姐,你呐 2025-01-10 12:58:10

您的网址,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: 等内容。请参阅属性列表编程指南

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:

<!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>

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:

1328633219

Then your code above would be able to get the number without any changes.

However, the following code might prevent your code from working:

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];

If label is nil, then timeInterval won't be set properly, because [label.text doubleValue] will also return nil. You might try instead:

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];

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.

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

Alternatively, you could upload a plist file and use something like NSDictionary dictionaryWithContentsOfURL: or NSArray arrayWithContentsOfURL:. See the property list programming guide.

流年里的时光 2025-01-10 12:58:10
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

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;
}

output: timeInterval 100.000000

行雁书 2025-01-10 12:58:10

您是否单步执行代码(使用 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 to countdown_timestamp.txt and just put your time interval value, by itself (e.g. "2.0") into it. Don't wrap it in HTML.

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