在解开GMSPlacesClient类型的可选值时,意外地发现了nil

时间:2016-06-20 06:14:01

标签: ios swift google-maps

我正试图将我的谷歌地图视图放在用户当前位置的中心但是我遇到麻烦,因为虽然我将gpsCoordinates分配给它,但是我将其命名为nil,而下面是我的viewdidload()中的代码查看控制器

var gmsPlace : GMSPlace?
var gpsCoordinates : CLLocationCoordinate2D?

override func viewDidLoad() {
    super.viewDidLoad()
    //placesClient = GMSPlacesClient()
    var placesClient = GMSPlacesClient.sharedClient()
    placesClient.currentPlaceWithCallback { (placeLikelihoods, error) -> Void in
        guard error == nil else {
            print("Current Place error: \(error!.localizedDescription)")
            return
        }
        var gmsPlace : GMSPlace?

        if let placeLikelihoods = placeLikelihoods {
            for likelihood in placeLikelihoods.likelihoods {
                gmsPlace = likelihood.place
                //print("Current Place name \(gmsPlace.name) at likelihood \(likelihood.likelihood)")
                //print("Current Place address \(gmsPlace.formattedAddress)")
                //print("Current Place attributions \(gmsPlace.attributions)")
                //print("Current PlaceID \(gmsPlace.placeID)")
                self.gpsCoordinates = (gmsPlace!.coordinate)

            }
            print(self.gpsCoordinates)

        }
    }
    let testObject = PFObject(className: "TestObject")
    testObject["foo"] = "bar"
    testObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
        print("Object has been saved.")
    }
    let camera = GMSCameraPosition.cameraWithTarget(gpsCoordinates!, zoom: 16.9)
    //let camera = GMSCamera
    print(camera)
    viewMap.camera = camera
    viewMap.myLocationEnabled = true
    viewMap.settings.myLocationButton = true


    let marker = GMSMarker()
    marker.position = self.gpsCoordinates!
    marker.title = "Newport Beach"
    marker.snippet = "California"
    marker.map = viewMap

    // Do any additional setup after loading the view, typically from a nib.
    print(url)
    print(videoData)
    print(doUpload)
    print(FriendsOrPublic)
    print(dataPath)
    if doUpload == true {
        Upload()
    }

   // Download()
}

2 个答案:

答案 0 :(得分:0)

您好,只是在视图中看到您的问题将会解决

答案 1 :(得分:0)

基本上,遇到的错误是由Implicitly Unwrapped Optional返回一个nil值引起的。

正如Optionals中所述:

  

如果在不提供默认值的情况下定义可选变量,则变量将自动设置为nil。

因此,为了防止您的应用崩溃,您需要打开一个可以安全或强行完成的可选值。

在此SO帖子What does “fatal error: unexpectedly found nil while unwrapping an Optional value” mean?中可以找到有关此类错误原因以及如何处理错误的详细说明。