2

我有一个方法用于在地图上设置和显示注释:

func setupPlacemark(place: Place, mapView: MKMapView) {

    guard let location = place.location else { return }

    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(location) { [unowned self] (placemarks, error) in

        if let error = error {
            print(error)
            return
        }

        guard let placemarks = placemarks else { return }

        let placemark = placemarks.first

        let annotation = MKPointAnnotation()
        annotation.title = place.name
        annotation.subtitle = place.type

        guard let placemarkLocation = placemark?.location else { return }

        annotation.coordinate = placemarkLocation.coordinate
        self.placeCoordinate = placemarkLocation.coordinate

        mapView.showAnnotations([annotation], animated: true)
        mapView.selectAnnotation(annotation, animated: true)
    }
}

当我运行泄漏工具时,我看到了这个泄漏:

图片

在这里你会看到哪一行代码不喜欢:

图片

我应该怎么做才能使这种泄漏消失?

4

1 回答 1

0

尝试更换:

mapView.showAnnotations([annotation], animated: true)
mapView.selectAnnotation(annotation, animated: true)

和 :

weak var weakMapView = mapView
weakMapView.showAnnotations([annotation], animated: true)
weakMapView.selectAnnotation(annotation, animated: true)

让我知道它是否有帮助,如果有帮助,我会添加解释。

于 2019-04-16T12:25:40.730 回答