-1
我想將Google API當前位置函數中出現的每個place.name添加到我的數組suggestedLocations
。當我打印下面的數組時,它總是空着。Swift .append到數組不工作
我的目標是有一個數組,列出當地的地方。我錯過了什麼?
class AddPersonVC: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var firstLocationNameBtn: UIButton!
@IBOutlet weak var secondLocationNameBtn: UIButton!
var manager = CLLocationManager()
var updateCount = 0
var zoomLevel = 200
var suggestedLocations = [String]()
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
// Users current location
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
mapView.showsUserLocation = true
manager.startUpdatingLocation()
} else {
manager.requestWhenInUseAuthorization()
}
// Find all local places
let placesClient = GMSPlacesClient()
placesClient.currentPlace(callback: { (placeLikelihoodList, error) -> Void in
if let error = error {
print("Pick Place error: \(error.localizedDescription)")
return
}
if let placeLikelihoodList = placeLikelihoodList {
for likelihood in placeLikelihoodList.likelihoods {
let place = likelihood.place
suggestedLocations.append(place.name)
print("Current Place name \(place.name) at likelihood \(likelihood.likelihood)")
//print("Current Place address \(place.formattedAddress)")
//print("Current Place attributions \(place.attributions)")
//print("Current PlaceID \(place.placeID)")
}
}
})
在***異步***數據請求完成之前,您可能正在打印數組的內容。 – luk2302
顯示您的打印位置。 – shallowThought
@ luk2302感謝您的建議。我能做些什麼來解決這個問題? 'print'顯示調試日誌中的列表,但當我嘗試在最後括號下面打印數組但仍然在ViewDidLoad中時,數組顯示爲空白。 – flakedev