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];
开发者_如何学JAVA [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?
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.
[self.mapView setRegion:region animated:YES];
精彩评论