如何在数据加载之前延迟解析这些必需的变量?

How to delay parsing these required variables before the data has loaded?

我正在从用于在 tableView 中显示信息的 google 位置 api 检索纬度和经度坐标。 tableView 结果基于这些坐标。我在下面的函数中将坐标解析到 tableView,但是在检索坐标之前 TableViewController 被推送。如何延迟推送直到坐标数据通过?谢谢

func placeSelected(place: Place) {
    println(place.description)

    var lat = 0.0
    var lng = 0.0

    place.getDetails { details in

        lat = details.latitude   // Convenience accessor for latitude
        lng = details.longitude  // Convenience accessor for longitude
    }

    let locationData = self.storyboard?.instantiateViewControllerWithIdentifier("TableViewController") as TableViewController

    locationData.searchLat = lat
    locationData.searchLng = lng

    self.navigationController?.pushViewController(locationData, animated: true)

}

您可以从 getDetails 方法的完成闭包中实例化并推送您的 TableViewController。

func placeSelected(place: Place) {
    println(place.description)

    place.getDetails { [weak self] details in
        let lat = details.latitude   // Convenience accessor for latitude
        let lng = details.longitude  // Convenience accessor for longitude

        if let self = self, locationData = self.storyboard?.instantiateViewControllerWithIdentifier("TableViewController") as? TableViewController {
            locationData.searchLat = lat
            locationData.searchLng = lng

            self.navigationController?.pushViewController(locationData, animated: true)
        }
    }
}

这假定闭包是在主线程上调用的。

您可能希望在 getDetails 方法为 运行 时显示某种 activity 指标。