【问题标题】:Swift .append to array not workingSwift .append 到数组不起作用
【发布时间】:2016-12-24 23:36:08
【问题描述】:

我想将来自 Google API 当前位置函数的每个 place.name 添加到我的数组 suggestedLocations。当我打印下面的数组时,它总是空出来的。

我的目标是有一个列出本地地点的数组。我错过了什么?

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 感谢您的建议。我能做些什么来解决这个问题? print 在调试日志中显示列表,但是当我尝试在最后的括号下方但仍在 ViewDidLoad 内打印数组时,数组显示为空白。

标签: arrays swift google-maps google-api


【解决方案1】:

根据您的评论,您对arrayprint 在最后的括号下方,但仍在viewDidLoad 之内。这就是它为空的原因,placesClient.currentPlace 没有完成运行,因为它是一个块方法。

您应该调用打印函数来显示最新的完成数组。尝试使用以下代码更新代码:

//remain the above code as it is
    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)")
        }
    self.printArray()  //Add a function call on what you want to do with the array here.
    }
})

func printArray() {
    print(suggestedLocations)
}

这只是向您展示如何获取打印数组的示例。如果你想重新加载tableView,也许你也可以在这里调用self.tableView.reloadData()

【讨论】:

    猜你喜欢
    • 2020-06-20
    • 1970-01-01
    • 2016-07-05
    • 2012-02-18
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    相关资源
    最近更新 更多