Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSString into NSTimeInterval

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

like image 529
AmiiQo Avatar asked Dec 28 '22 06:12

AmiiQo


2 Answers

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.

like image 193
mrb Avatar answered Dec 29 '22 20:12

mrb


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

like image 37
Steve Ham Avatar answered Dec 29 '22 18:12

Steve Ham