在展开可选值时意外发现nil:查询功能仅在一个实例

时间:2016-07-15 06:19:27

标签: ios swift google-maps parse-platform xcode7

我有这个功能从我的Parse服务器检索视频:

func downloadVideo(marker: GMSMarker)
{
    print("printing")
    print(markerLat)
    print(markerLong)
    print("A")
    let query = PFQuery(className:"UploadedVideoCurrent")
    print("b")
    //query.whereKey("GPS", equalTo: PFGeoPoint(latitude: markerLat, longitude: markerLong))
    query.findObjectsInBackgroundWithBlock { (objects, error) in
        print("c")
        if(error == nil)
        {
            print("d")

            for object in objects!
            {


               print("made it")

                self.file = (object.objectForKey("VideoFile") as! PFFile)
                //self.thumbnail = (object.objectForKey("Thumbnail") as! PFFile)
                self.userofVideo = object.objectForKey("User")
                self.hour = object.objectForKey("Hour")
                self.minutes = object.objectForKey("minutes")
                self.street = object.objectForKey("Location")
                print(self.file!.url)

                self.downloadedurl = self.file!.url


                var tempLat = object.objectForKey("GPS")!.latitude as Double
                var tempLong = object.objectForKey("GPS")!.longitude as Double

                //print(tempLong)
                //print(tempLat)
                print(tempLat.roundToPlaces(3))
                print(self.markerLat.roundToPlaces(3))
                print(tempLong.roundToPlaces(3))
                print(self.markerLong.roundToPlaces(3))
                if(tempLat.roundToPlaces(3) == self.markerLat.roundToPlaces(3) && tempLong.roundToPlaces(3) == self.markerLong.roundToPlaces(3)){
                    print("success")
                    self.postsLat = tempLat
                    self.postsLong = tempLong
                }
                //print(self.postsLong)
                //print(self.postsLat)
                //var coordinates = [postsLat, postsLong, x]

            }

        }
        else
        {
            print("didn't make it")
            print(error)
        }
    }
}

然而,它仅适用于我调用它的某些情况,而我似乎无法理解为什么。在viewdidload和一个IBAction中,我点击downloadvideo运行良好:

@IBAction func snapNext(sender: AnyObject) {
    if self.z != 1{
        x = x! + 1
        var count = markers.count
        print("This is the markers count: \(markers.count)")
        print(x)
        if x < markers.count {

        self.viewMap.camera = GMSCameraPosition.cameraWithTarget(self.markers[x!], zoom: 16.9)
        } else {
            self.viewMap.camera = self.camera!
            x = 0
        }
        //viewMap.camera = marker.userData[0]
    }else{
        self.viewMap.camera = self.camera!

    }
    var count = 0
    if markers.count > 3 {
    while count < 3 {


        self.marker = GMSMarker(position: markers[x!])
        print(self.marker)
        print(markers[x!])
        downloadVideo(marker)
        count = count + 1


    }
    }else if markers.count == 3 {



            self.marker = GMSMarker(position: markers[2])
            print(self.marker)
            print(markers[2])
            downloadVideo(marker)
            self.marker = GMSMarker(position: markers[1])
            print(self.marker)
            print(markers[1])
            downloadVideo(marker)
            self.marker = GMSMarker(position: markers[0])
            print(self.marker)
            print(markers[0])
            downloadVideo(marker)


    }else if markers.count == 2{
        self.marker = GMSMarker(position: markers[1])
        print(self.marker)
        print(markers[1])
        downloadVideo(marker)
        self.marker = GMSMarker(position: markers[0])
        print(self.marker)
        print(markers[0])
        downloadVideo(marker)


    }else if markers.count == 1 {
        self.marker = GMSMarker(position: markers[0])
        print(self.marker)
        print(markers[0])
        downloadVideo(marker)
    }else {

    }


}

然而,当我通过点击GMSmarker来调用它时,只是在打印B作为控制台的下一个输出的行之后崩溃或跳过查询是致命错误:在运行以下代码后意外地发现nil:

func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
    if z == 0 {


    let lat = marker.position.latitude as Double
    let long = marker.position.longitude as Double
    markerLat = lat.roundToPlaces(6)
    markerLong = long.roundToPlaces(6)
    //self.marker = GMSMarker(position: markers[2])
    x = 1
        downloadVideo(marker)
        if(self.postsLat!.roundToPlaces(3) == self.markerLat.roundToPlaces(3) && self.postsLong!.roundToPlaces(3) == self.markerLong.roundToPlaces(3))
        {

            self.performSegueWithIdentifier("playerSegue", sender: nil)
            print("a")
            file!.getDataInBackgroundWithBlock({ (data, error) in
                if error == nil
                {

                    self.mp4 = data
                    print(data)
                    print(self.mp4)
                    print("a")
                }
            })
        }else{
            print("not playing")
            print(self.postsLat!.roundToPlaces(3))
            print(self.markerLat.roundToPlaces(3))
            print(self.postsLong!.roundToPlaces(3))
            print(self.markerLong.roundToPlaces(3))
        }
    }else{
    }
    return true
}

考虑到查询不使用甚至被调用的变量而且c永远不会被打印出来,我甚至不知道这是怎么回事。

0 个答案:

没有答案