4

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

AmiiQo
  • 353
  • 1
  • 7
  • 17
  • what's the problem? it seems ok – Inder Kumar Rathore Feb 07 '12 at 17:57
  • How is it not working? Have you verified (by displaying or looking with the debugger) that the string value is coming in properly? Have you verified that the label text is being set properly? – Nathan S. Feb 07 '12 at 17:58
  • I get a very strange output for the countdown itself if I launch the app. I use the label just to convert it into the NSTimeInterval.. – AmiiQo Feb 07 '12 at 17:59

3 Answers3

7

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.

mrb
  • 3,281
  • 1
  • 20
  • 31
  • I tried it with a txt file, that should work better than an htmlfile, right? – AmiiQo Feb 07 '12 at 18:09
  • If your txt file only contains the number with nothing else, yes. Since you're grabbing the number from `label.text`, you have to make sure label is not nil. You could try `double timeInterval = [string doubleValue]` instead. – mrb Feb 07 '12 at 18:15
  • It downloads now the proper file: "Countdown[1538:f803] Downloaded file with contents: 1354904016" but the label isn't showing anything... – AmiiQo Feb 07 '12 at 18:50
  • It sounds like you just need to make sure the label is set up properly. eg. If you're using a nib/xib, make sure it's connected to the right outlet. You can use something like `NSLog(@"Label is %@", label);` to see if it's nil or something else, but since you have the correct contents now, you should be able to convert it to an `NSTimeInterval` and do whatever else you want with it. – mrb Feb 07 '12 at 19:19
  • It tells me that label is (null), strange I connected everything like it has to be connected. I connected dateLabel with the label in the xib file and nothing else – AmiiQo Feb 07 '12 at 19:24
  • Could it be that you're not setting dateLabel in the code, but just label? Maybe you need something like dateLabel.text = ... – mrb Feb 07 '12 at 20:39
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;
}

output: timeInterval 100.000000

Steve Ham
  • 3,067
  • 1
  • 29
  • 36
1

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.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • I added the updateLabel so you can see where I use destinationDate... But it still doesn't seem to work with the txt file – AmiiQo Feb 07 '12 at 18:06
  • It sounds like you have a broken IBOutlet. I bet label is nil. So any code that tries to take a value from label.text is going to return a zero or a nil. – Duncan C Jun 26 '12 at 01:07