Reputation: 11
I have a "local declaration hides instance variable" error for “secondsLeft” and “unused variable” for hours, minutes and seconds. Thank you in advance for any help you can provide on this one.
.h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "BT_viewController.h"
@interface BT_screen_blank : BT_viewController {
NSTimer *timer;
IBOutlet UILabel *myCounterLabel;
}
@property (nonatomic, retain) UILabel *myCounterLabel;
@property (nonatomic) int secondsLeft;
@property (nonatomic) int minutes;
@property (nonatomic) int hours;
@property (nonatomic) int seconds;
-(void)updateCounter:(NSTimer *)theTimer;
-(void)countdownTimer;
@end
.m file
@implementation BT_screen_blank
@synthesize myCounterLabel;
@synthesize secondsLeft, hours, minutes, seconds;
//viewDidLoad
-(void)viewDidLoad{
[BT_debugger showIt:self:@"viewDidLoad"];
[super viewDidLoad];
int hours, minutes, seconds;
int secondsLeft;
secondsLeft = 16925;
[self countdownTimer];
}
- (void)updateCounter:(NSTimer *)theTimer {
if(secondsLeft > 0 ){
secondsLeft -- ;
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
myCounterLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
}
else{
secondsLeft = 16925;
}
}
Upvotes: 1
Views: 1821
Reputation: 884
"local declaration hides instance variable" error for “secondsLeft”
You redeclared int secondsLeft;
in the .m file
So on this line secondsLeft = 16925;
the compiler is storing 16925 in the local variable in -(void)viewDidLoad
method instead of int secondsLeft;
which is declared in .h file
You should remove the redeclaration of
int hours, minutes, seconds;
int secondsLeft;
in the .m file. Alternatively you can use another variable names
“unused variable” for hours, minutes and seconds.
Well this is just a warning to highlight that you never use these variables int hours, minutes, seconds;
which are declared in -(void)viewDidLoad
method
Upvotes: 0
Reputation: 89509
Get rid of these lines from your "viewDidLoad
" function:
int hours, minutes, seconds;
int secondsLeft;
Those two lines are exactly what's generating the "local declaration hides instance variable
" errors you're seeing.
and like edzio says, use "self.
" in front of any properties you're referencing. +1 to him!
Upvotes: 1
Reputation: 4140
You have to call variables with self. For example:
self.hours = self.secondsLeft / 3600;
also if you want to declare later variable with the same name, use other name, for example:
int hours_tmp;
Upvotes: 1