0

If I have a string from key and the string is a timer (12:00) how to add 1 minute to the timer, so the label will show 12:01:

NSString string = [subDict objectForKey:@"1"];

NSScanner timeScanner=[NSScanner scannerWithString:string];
int hours,minutes;
[timeScanner scanInt:&hours];
[timeScanner scanString:@":" intoString:nil]; 
[timeScanner scanInt:&minutes];

Thanks

2 Answers2

0

try like this,

 self.mylabel.text= [NSString stringWithFormat:@"%02d:%02d",[[string substringToIndex:2] intValue],[[string substringFromIndex:3] intValue]+1];
Balu
  • 8,470
  • 2
  • 24
  • 41
0

Make sure to take care of the carry. As to a simple solution, you don't even need a scanner:

NSString *timeStr = @"23:59";

NSArray *comps = [timeStr componentsSeparatedByString:@":"];
int h = [comps[0] intValue];
int m = [comps[1] intValue];

h += (m + 1) / 60;
h %= 24;

m = (m + 1) % 60;

NSLog(@"The new time is: %02d:%02d", h, m);
  • It isnt working when I do: NSString* stringtime = [subDict objectForKey:@"1"]; NSArray *comps = [stringtime componentsSeparatedByString:@":"]; int h = [comps[0] intValue]; int m = [comps[1] intValue]; h += (m + 1) / 60; h %= 24; m = (m + 1) % 60; //Give it to label self.num.text = stringtime; – user2310932 Jul 16 '13 at 07:52
  • @user2310932 `NSString* stringtime = [subDict objectForKey:@"1"]; NSArray *comps = [firstPray componentsSeparatedByString:@":"];` - you are not decomposing the string you are getting. Also, learn some basic debugging. This is getting quite tiring for me. –  Jul 16 '13 at 07:53