【问题标题】:Unexplained exception using the Google Places iOS SDK after upgrade to Xcode 8 and Swift 3.0升级到 Xcode 8 和 Swift 3.0 后使用 Google Places iOS SDK 出现无法解释的异常
【发布时间】:2016-09-16 17:37:13
【问题描述】:

在将其转换为 swift 3.0 后,我什至无法让演示代码工作。我已经尝试了我能想到的一切都无济于事。

异常非常无用,堆栈跟踪也是如此。 (见截图)

这是 swift 2.3 代码,运行良好:

@IBAction func getCurrentPlace(sender: UIButton) {

    placesClient?.currentPlaceWithCallback({
    (placeLikelihoodList: GMSPlaceLikelihoodList?, error: NSError?) -> Void in
        if let error = error {
            print("Pick Place error: \(error.localizedDescription)")
            return
        }

        self.nameLabel.text = "No current place"
        self.addressLabel.text = ""

        if let placeLikelihoodList = placeLikelihoodList {
            let place = placeLikelihoodList.likelihoods.first?.place
            if let place = place {
                self.nameLabel.text = place.name
                self.addressLabel.text = place.formattedAddress!.componentsSeparatedByString(", ")
                    .joinWithSeparator("\n")
            }
        }
    })
}

这是 swift 3.0 中的等价物:

@IBAction func go(_ sender: AnyObject) {
    var placesClient: GMSPlacesClient?
    placesClient = GMSPlacesClient.shared()
    placesClient?.currentPlace(callback: {
        (placeLikelihoodList: GMSPlaceLikelihoodList?, error: NSError?) in

        if let error = error {
            print("Pick Place error: \(error.localizedDescription)")
            let alert = UIAlertController(title: "Error", message: "Couldn't find a location", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)

            return
        }

        if let placeLikelihoodList = placeLikelihoodList {
            let place = placeLikelihoodList.likelihoods.first?.place
            if let place = place {
                self.testlabel.text = place.name
            }
        }


        } as! GMSPlaceLikelihoodListCallback)
}

异常让我停在这里:as! GMSPlaceLikelihoodListCallback

有人知道发生了什么吗?

【问题讨论】:

  • 我没有使用过这些,但是为什么需要强制转换(as! GMSPlaceLikelihoodListCallback)?
  • 我也是这么想的。没有它它不会编译,并且有一个来自 xcode 的友好的“修复它”提示可以强制添加它。
  • 你可能在回调参数中需要Error而不是NSError
  • @dan 这完全是问题所在!我不敢相信我没听懂!

标签: ios google-places-api swift3 xcode8 google-places


【解决方案1】:
    var placesClient: GMSPlacesClient?
    placesClient = GMSPlacesClient.shared()
    placesClient?.currentPlace(callback: { (place, error) in
        if let error = error {
            print("Pick Place error: \(error.localizedDescription)")
            let alert = UIAlertController(title: "Error", message: "Couldn't find a location", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)

            return
        }

        if let placeLikelihoodList = place {
            let place = placeLikelihoodList.likelihoods.first?.place
            if let place = place {
                self.lblOrigem.text = place.formattedAddress
            }
        }
    })

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多