Ok here is the problem, I have three NSStrings with int values, when the view loads this needs to run:
NSCalendar* gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int uintFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents* differenceComponents = [gregorian components:uintFlags
fromDate:quitDate toDate:nowDate options:0];
NSString *hours1 = [NSString stringWithFormat:@"%d",[differenceComponents hour]];
hours = hours1;
NSString *minutes1 = [NSString stringWithFormat:@"%d",[differenceComponents minute]];
minutes = minutes1;
NSString *seconds1 = [NSString stringWithFormat:@"%d",[differenceComponents second]];
seconds = seconds1;
Those NSString now have int values in it, so I can’t set int’s to static any suggestions?
I wanted to do this way so I won’t it…
- (void)updater:(id)sender {
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self
selector:@selector(timer:) userInfo:nil repeats:YES];
}
- (void)timer:(id)sender {
// I get error right here that says (initialize
// element is not a compile-contstant)
static int hour = hours.intValue;
static int minute = minutes.intValue;
static int second = seconds.intValue;
NSString *sec = [NSString stringWithFormat:@"%i",second];
if (seconds1 < 10) {
sec = [NSString stringWithFormat:@"0%i",second];
}
NSString *min = [NSString stringWithFormat:@"%i",minute];
if (minutes1 < 10) {
min = [NSString stringWithFormat:@"0%i",minute];
}
NSString *hours5 = [NSString stringWithFormat:@"%i",hour];
NSString *timerTime = [NSString stringWithFormat:@"%@:%@:%@" ,hours5 ,min ,sec];
label1.text = timerTime;
seconds1 ++;
if (seconds1 > 59) {
seconds1 = 00;
minutes1 ++;
}
if (minutes1 > 59) {
minutes1 = 00;
hours1 ++;
}
}
Why do you want to use
static? I don’t get the point of making those variablesstatic.And why using
NSStringto encapsulate yourintvalues as well?!?? Why not store the int values directly?Actually you shouldn’t either rely of adding the seconds yourself at each execution of the
timermethod, because anNSTimercan drift, so after some time (quite long time, sure, but still) you can have this drift affect the “seconds” part. Better recompute the timeInterval each time. And why bother adding the leading “0” yourself, when you could use%02iformat instead?Actually your code can be as simple and concise as this: