how to unset/kill static int variables in objective c
I am using static int variables but having some problem. Problem is that its not getting reset. If I leave this page and come again then I found previous values while I have reset it.
Please give me solution.
here is my code
- (void)updateQuestion:(NSTimer *)theTimer {
static int questionCounter = 1;
questionCounter += 1;
count=(questionCounter%QUESTION_TIME_LIMIT);
tfLeftTime.text=[NSString stringWithFormat:@"%d",QUESTION_TIME_LIMIT];
tmLeftTime=[[NSTimer alloc] ini开发者_StackOverflowt];
tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
[self playMusic];
if (questionCounter>QUESTION_PER_LEVEL) {
if ([tmQuestion isValid]) {
[tmQuestion invalidate];
questionCounter=1;
count=1;
tmQuestion=nil;
[self showAdvertisement];
}
}
}
Thank & Regards Shivam
You can also use a instance variable to have the effect you want.
The counter will remain as long as the object lives, and is destroyed when you no longer need it.
- You cannot declare a static variable in one method and use in in another one. I assume you declared it in the file, outside of any methods. Use instance variables instead.
You have a memory leak at:
tmLeftTime=[[NSTimer alloc] init];
tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
First allocation of the tmLeftTime is never released.
[NSTimer scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:] gives you an autoreleased timer.
精彩评论