mapView.annotations.count 在 swift 中不为 0

mapView.annotations.count is not 0 in swift

我有以下代码:

@IBAction func countAnnotations(sender: AnyObject) {
    print("Annotations Count = \(mapView.annotations.count)")

    if mapView.annotations.count == 0 {
        print("No annotations")
    }else{
        print("1 or more annotations")
    }
}

这是为了检查我的地图视图上有多少注释。

当我开始使用以下代码检索用户位置时出现问题:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    //Update the current location
    currentLocation = manager.location!.coordinate
    print("locations = \(currentLocation.latitude) \(currentLocation.longitude)")
}

出于某种原因,这似乎算作注释。如何实际检测是否放置了实际注释?

通过简单地添加一个 currentLocationVisible = false 布尔变量来修复。然后设置并检查它。

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      //Update the current location
      currentLocation = manager.location!.coordinate
      print("locations = \(currentLocation.latitude) \(currentLocation.longitude)")
      currentLocationVisible = true
}

现在只是检查变量。

if (mapView.annotations.count == 0 || currentLocationVisible == true)

就这么简单!