运行Query Parse时出错

时间:2015-11-17 01:34:43

标签: swift swift2

Query PARSE

遇到错误:

  

致命错误:在解包可选值时意外发现nil

下面是我点击退出应用程序时抓取应用程序的致命错误的代码

CODE:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let location: CLLocationCoordinate2D = manager.location!.coordinate

    print("location = \(location.latitude) \(location.longitude)")

    latitude = location.latitude
    longitude = location.longitude

    // Mostrar o motorista que aceitou o chamado

    let query = PFQuery(className: "RiderRequest")

    **query.whereKey("username", equalTo: PFUser.currentUser()!.username!)**
    query.findObjectsInBackgroundWithBlock { (objects:[PFObject]?, error: NSError?) -> Void in

        if error == nil {

            if let objects = objects as [PFObject]!{

                    for object in objects {

                        // checar se alguém respondeu o chamado
                        if let driverUsername = object["driverResponded"] {

                            let driverQuery = PFQuery(className: "driverLocation")
                            driverQuery.whereKey("username", equalTo: driverUsername)
                            driverQuery.findObjectsInBackgroundWithBlock {(objects:[PFObject]?, error:NSError?) -> Void in

                                if error == nil {

                                    if let objects = objects as [PFObject]?{

                                        for object in objects {

                                            if let driverLocation = object["driverLocation"] as? PFGeoPoint {

                                                // Criar as distâncias usuario e motorista
                                                let driverCLLocation = CLLocation(latitude: driverLocation.latitude, longitude: driverLocation.longitude)
                                                let userCLLocation = CLLocation(latitude: location.latitude, longitude: location.longitude)

                                                let distanceMeters = userCLLocation.distanceFromLocation(driverCLLocation)
                                                let distanceKm = distanceMeters / 1000
                                                let roundedDistance = Double(round(distanceKm * 10) / 10)


                                                // exibir a localização do usuário
                                                // altera a Label para o usuário

                                                self.ChamarButton.setTitle("\(driverUsername) a \(roundedDistance)Km", forState: .Normal)
                                                self.driverOnTheWay = true

                                                let latDelta = abs(driverLocation.latitude - location.latitude) * 2 + 0.001
                                                let lonDelta = abs(driverLocation.longitude - location.longitude) * 2 + 0.001

                                                // mostrar os 2 no mapa
                                                let center = CLLocationCoordinate2D (latitude: location.latitude, longitude: location.longitude)
                                                let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta))


                                                self.Map.setRegion(region, animated: true)
                                                self.Map.removeAnnotations(self.Map.annotations)

                                                // Usuario
                                                let pinLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.latitude, location.longitude)
                                                let pinAnnotation = MKPointAnnotation ()
                                                pinAnnotation.coordinate = pinLocation
                                                pinAnnotation.title = "Você"

                                                self.Map.addAnnotation(pinAnnotation)

                                                // Motorista
                                                _ = CLLocationCoordinate2DMake(driverLocation.latitude, driverLocation.longitude)
                                                let driverpinAnnotation = MKPointAnnotation ()
                                                driverpinAnnotation.coordinate = pinLocation
                                                driverpinAnnotation.title = "Motorista"

                                                self.Map.addAnnotation(driverpinAnnotation)

                                            }

                                        }
                                    }

                                }else{

                                    print(error)
                                }

                        }
                    }
                }

            }

        }else{

            print(error)
        }

    }

1 个答案:

答案 0 :(得分:0)

请注意执行停止的位置。您的错误意味着您正在使用!强制解包可选项,但该选项等于nil。所以这很简单:在你申请之前检查它是否为零!如果它是意外的零,那么你需要更深入地找出原因。