Convert NSString to NSTimeInterval

I tried to do a countdown with NSTimeInterval. But I want to be able to change the interval without releasing updates every time. So I tried to import Timeinterval from my site. I saved the numbers for NSTimeInterval in NSString and now I want to convert them to NSTimeInterval in order to implement it in the countdown code ...

... but it does not work. 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 is as follows:

  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 dateLabel does nothing ...

+4
source share
3 answers

Your URL, http://gymnasium2.ai.ch/~mensa/countdown_timestamp.html , is a complete HTML file. NSString initWithContentsOfURL: will return a string containing all its contents:

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

It cannot be converted to double; you will need to parse the HTML code, which can be quite large.

It would be easier to place a simple file containing only a number:

 1328633219 

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

However, the following code may interfere with your code:

 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 will not be set properly, because [label.text doubleValue] will also return nil . Instead, you can try:

 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 useful to reset the breakpoint or add an NSLog call after retrieving the file so you can see what is happening.

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

Alternatively, you can load the plist file and use something like NSDictionary dictionaryWithContentsOfURL: or NSArray arrayWithContentsOfURL: See the list property list guide .

+7
source
 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

+2
source

Have you generally looked at the code (using the Xcode debugger) to see which line of your code gets unexpected results? You never use destinationDate , and in fact, what you should go to the last line of code is:

timer = [NSTimer scheduledTimerWithTimeInterval:intervalForTimer target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];

In addition, the place where you get your time interval is a web page with HTML tags around it. Change countdown_timestamp.html to countdown_timestamp.txt and just put your time interval value separately (for example, " 2.0 "). Do not wrap it in HTML.

+1
source

Source: https://habr.com/ru/post/1395283/


All Articles