AmiiQo
AmiiQo

Reputation: 353

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

Upvotes: 4

Views: 11480

Answers (3)

Steve Ham
Steve Ham

Reputation: 3154

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

Upvotes: 2

mrb
mrb

Reputation: 3330

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.

Upvotes: 7

Michael Dautermann
Michael Dautermann

Reputation: 89559

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.

Upvotes: 1

Related Questions