startuprob
startuprob

Reputation: 1915

MKMapView Error

I guess error is too strong as it works exactly like I want it to, but I am getting a warning message that is troubling. I am adding a UIViewController to a TabBarController and all I want it to do is display the Map View and then zoom in on the user when the user clicks on that tab. I built the xib and the only editing I did was to drag an MKMapView over and hook up the outlet, as well as the delegate. Other than that, this is all the implementation code:

- (void)viewDidLoad
{

    [super viewDidLoad];

    [mapView setShowsUserLocation:YES];

}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [mapView setRegion:region animated:YES];

}

The problem is that I am getting a yellow alarm error on the final line that says, "Local declaration of "mapView" hides the instance variable."

However, when I run the app, it seems to be working correctly. What could this error be referring to?

Upvotes: 0

Views: 372

Answers (2)

runmad
runmad

Reputation: 14886

[self.mapView setRegion:region animated:YES];

Upvotes: 0

Joris Ooms
Joris Ooms

Reputation: 12038

This is happening because you declared your MKMapView object as mapView. Look at the method declaration:

- (void)mapView:(MKMapView *)mapView

it uses the same name as your own variable. This is where the compiler seems to run into a problem. I've had this a couple of times myself now, and my solution was to change my variable name (you could use worldView, for example).

I think you could also solve it with self. Like in other languages, you would use this.variableName, I think you can do self.variableName to indicate that this one is 'your' variable. If that makes sense?

When I declare my own methods, I usually do like this:

 - (void)feedDog:(Dog *)aDog;

and then my variable would be called Dog to avoid confusion.

Just what I do. If anyone has anything to add to this, feel free to. I'm still fairly new to this, but I did run into this error a couple of times now.

Upvotes: 1

Related Questions