试图运行PFQuery,这将导致只返回一个结果,但我所环境的参数不够具体

时间:2016-09-03 15:06:14

标签: ios arrays swift parse-platform pfquery

我正在尝试运行PFQuery,以便为存储在服务器中的地址查找特定结果。但是,当我尝试一个特定的地址时,我总是返回0.这个类拥有的字段是objectId,startTime和title。我不确定这是否是提出问题的最佳方式,但是是否有一种通用格式可以让我查询存储的最新地址并将其作为代码中的字符串返回?这是我到目前为止在我的加载屏幕视图控制器中编写的内容,该控制器旨在根据用户/他的位置放置用户。

导入UIKit 导入CoreLocation import Parse

类LoadViewController:UIViewController,CLLocationManagerDelegate {

var locationManager = CLLocationManager()
var lastUserLocation = "nil"

  var address = ""
var startTime = ""

func findEventProximity(eventObjectId: String){

    var eventObjectIdQuery = PFQuery(className: "Events")
    eventObjectIdQuery.orderByDescending("createdAt")
    eventObjectIdQuery.whereKey("objectId", equalTo: eventObjectId)
    eventObjectIdQuery.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
        if error == nil  {
            print(objects!.count)
            if let objects = objects as [PFObject]? {
                for object in objects {
                    object["objectId"] = selectedEventObjectId

                    /*   need to decide wether to store Time  in createNewEvent  as NSDate or just string
                     if object["date"] != nil {
                     self.eventDateLabel.text = object["date"] as! NSDate
                     }
                     */

                  print("\(object["address"] as! String)")
                }
            } else {
                print(error)
            }
        }






    })

}


override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()

    findEventProximity(selectedEventObjectId)



    // Do any additional setup after loading the view.
}

/*MARK: LOCATION*/
/*MARK: LOCATION*/
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    self.locationManager.stopUpdatingLocation()

    let userLocation: CLLocation = locations[0]

    let userLocationLat = locations[0].coordinate.latitude
    let userLocationLong = locations[0].coordinate.longitude
    var location = CLLocation(latitude: userLocationLat, longitude: userLocationLong)


    print("\(userLocationLat), \(userLocationLong)")





    let locationsToCheck: [CLLocation] = [
        CLLocation(latitude: lat, long),
        CLLocation(latitude: lat, long),
        CLLocation(latitude: lat, long),
        CLLocation(latitude: lat, long)
    ]
    let locationTags: [String] = [
        "P1",
        "P2",
        "P3",
        "P4"
    ]
    var closestInMeters: Double = 50000.0
    var closestIndex: Int = -1

    for (locationIndex, potentialLocation) in locationsToCheck.enumerate() {
        let proximityToLocationInMeters: Double = userLocation.distanceFromLocation(potentialLocation)

        if proximityToLocationInMeters < closestInMeters {
            closestInMeters = proximityToLocationInMeters
            closestIndex = locationIndex

        }
    }

    if (closestIndex == -1) {
        self.lastUserLocation = "Other"

        //            SCLAlertView().showError("No Filter Range", subTitle: "Sorry! We haven’t got a Filter Range for you yet, email us and we’ll get right on it.").setDismissBlock({ () -> Void in
        //                selectedLocation = self.locationObjectIdArray[0]
        //                self.performSegueWithIdentifier("eventTableSegue", sender: self)
        //            })
        //                        self.activityIndicator.stopAnimating()
        //            UIApplication.sharedApplication().endIgnoringInteractionEvents()

    } else if locationTags[closestIndex] != self.lastUserLocation {
        self.lastUserLocation = locationTags[closestIndex]

    }


}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

0 个答案:

没有答案