MKMapView错误
我猜错误太严重了,因为它的工作原理与我想要的完全一样,但我收到了一条令人不安的警告消息。我将 UIViewController 添加到 TabBarController,我想要它做的就是显示地图视图,然后在用户单击该选项卡时放大用户。我构建了 xib,我所做的唯一编辑是将 MKMapView 拖过来并连接插座和委托。除此之外,这是所有实现代码:
- (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];
}
问题是我在最后一行收到黄色警报错误,其中显示““mapView”的本地声明隐藏了实例变量”。
但是,当我运行该应用程序时,它似乎工作正常。这个错误可能指的是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为您将 MKMapView 对象声明为
mapView
。查看方法声明:- (void)mapView:(MKMapView *)mapView
它使用与您自己的变量相同的名称。这就是编译器似乎遇到问题的地方。我自己已经遇到过几次这种情况,我的解决方案是更改我的变量名称(例如,您可以使用 worldView)。
我想你也可以自己解决。与其他语言一样,您可以使用
this.variableName
,我认为您可以使用self.variableName
来指示这是“您的”变量。如果这有道理吗?当我声明自己的方法时,我通常这样做:
然后我的变量将被称为
Dog
以避免混淆。这就是我所做的。如果有人对此有任何补充,请随时补充。我对此还很陌生,但我现在确实遇到过这个错误几次。
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 doself.variableName
to indicate that this one is 'your' variable. If that makes sense?When I declare my own methods, I usually do like this:
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.