将NSString转换为NSTimeInterval

Ami*_*iQo 4 iphone nsstring nstimeinterval xcode4 ios5

我试着用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];
Run Code Online (Sandbox Code Playgroud)

编辑:

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

}
Run Code Online (Sandbox Code Playgroud)

我的新代码看起来像这样:

    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];
Run Code Online (Sandbox Code Playgroud)

但是dateLabel没有做任何事......

mrb*_*mrb 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>
Run Code Online (Sandbox Code Playgroud)

这不能转换成双倍; 你需要解析HTML,这可能是相当多的工作.

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

1328633219
Run Code Online (Sandbox Code Playgroud)

那么上面的代码就可以获得数字而无需任何更改.

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

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];
Run Code Online (Sandbox Code Playgroud)

如果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];
Run Code Online (Sandbox Code Playgroud)

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

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

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