mrorgon
mrorgon

Reputation: 65

Local declaration of "speed view" hides instance variable

So I have been searching in a few hours for why my iPhone app hates me. This is the error I get: Warning: local declaration of 'speedView' hides instance variable. Here is my .m file

@implementation MainViewController
@synthesize speedCount;
@synthesize speedView;
@synthesize popoverController;

- (void)setspeedView:(UILabel *)speedView
{
    [speedView setText: [NSString stringWithFormat:@"%d",speedCount]];
    speedCount = 0;
    speedCount++;
}

.h file

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MainViewController : UIViewController <LoginDelegate,WEPopoverParentView,PopoverControllerDelegate,MainMenuDelegate,MKMapViewDelegate,UIActionSheetDelegate,UIAccelerometerDelegate, CLLocationManagerDelegate>
{
    AppDelegate *appDelegate;
    IBOutlet MKMapView *userMap;
    IBOutlet UILabel *speedView;
    CLLocationManager *locationManager;
}

@property (strong, nonatomic) IBOutlet UILabel *speedView;
@property(nonatomic) int speedCount;

I really don't understand why it says that I am hiding the instance variable.

Upvotes: 2

Views: 232

Answers (1)

Gabriele Petronella
Gabriele Petronella

Reputation: 108111

You have a ivar (an instance variable) called speedView.

In your method

- (void)setspeedView:(UILabel *)speedView

speedView is a local variable whose name clashes with the ivar.

If you are using a modern version of the compiler just remove the @synthesize directive.

It will be automatically added by the compiler in this form

@synthesize speedView = _speedView

which will create the ivar _speedView, whose name doesn't clash anymore with the local variable.

Also note that declaring both the instance variable and the property is redundant. The ivar will be automatically created by the (implicit) @synthesize directive.

Here's a "modern" version of your class:

.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MainViewController : UIViewController <LoginDelegate,WEPopoverParentView,PopoverControllerDelegate,MainMenuDelegate,MKMapViewDelegate,UIActionSheetDelegate,UIAccelerometerDelegate, CLLocationManagerDelegate>

@property (strong, nonatomic) IBOutlet UILabel *speedView;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) IBOutlet MKMapView *userMap;
@property (strong, nonatomic) AppDelegate *appDelegate;
@property (nonatomic) int speedCount;

.m

@implementation MainViewController

- (void)setspeedView:(UILabel *)speedView {
    [speedView setText:[NSString stringWithFormat:@"%d", self.speedCount]];
    self.speedCount = 0;
    self.speedCount++;
}

Please note:

  • properties are nice: use them whenever you can
  • @synthesize is implicit
  • the implicit version of @sythesize declares a _ivar for the property ivar
  • always access variables through the getters/setters, i.e. self.ivar, a part from init methods. If you need to access the var directly use _ivar or self->_ivar

As a final remark, this looks a bit weird

self.speedCount = 0;
self.speedCount++;

and it could be replaced with

self.speedCount = 1;

Are you sure it's what you mean? Also, as noted in the comments by others, you are not using the method parameter speedView. That smells bad and you may want to double check your implementation.

Upvotes: 3

Related Questions