代码写久了,连最基本的语法都忘记了。

    for (id obj in self.mapView.annotations) {
        
        if ([obj class] == [MKUserLocation class]) {
            break;
        }
        [self.mapView removeAnnotation:obj];
    }

这是项目中的一段代码,目的是去除自己当前位置意外的annotationView,结果总是非自己所愿。因为是简单不过的代码,没有多想,加入log才发现,整个循环并不能总是执行完毕,大部分都会在中间的某个地方跳出循环。这才注意到break这个关键字,想起来是中断循环的,转而换了continue,效果就对了。

 

正确的:

    for (id obj in self.mapView.annotations) {
        
        if ([obj class] == [MKUserLocation class]) {
            continue;
        }
        [self.mapView removeAnnotation:obj];
    }

or

    for (id obj in self.mapView.annotations) {
        
        if ([obj class] != [MKUserLocation class]) {
            [self.mapView removeAnnotation:obj];
        }
    }

 

相关文章:

  • 2021-10-27
  • 2021-12-20
  • 2022-02-26
  • 2022-12-23
  • 2021-12-02
  • 2021-07-21
猜你喜欢
  • 2021-05-15
  • 2021-12-19
  • 2021-09-24
  • 2022-02-17
  • 2022-01-09
  • 2021-06-23
  • 2022-02-25
相关资源
相似解决方案