在Swift中比较NSDates

时间:2015-09-23 20:10:18

标签: swift parse-platform mkmapview nsdate swift2

我正在创建一个应用程序,用户可以在地图上创建可作为注释显示的事件。一旦当前时间大于用户创建的结束时间,我需要永久删除注释。现在结束时间以字符串形式给出,然后我将它与我转换为字符串的当前时间进行比较。它似乎总是有效,但自从事件被删除后几个小时已经消失,它总是重新出现。

这是我目前的方法:

override func viewDidLoad() {
    super.viewDidLoad()

   //*********** Set up Time

    let date = NSDate()
    let formatter = NSDateFormatter()
    formatter.timeStyle = .ShortStyle
    let currentTimeString = formatter.stringFromDate(date)


    //Return Short Time String
    print("The time is \(currentTimeString)")



    //*********** Set up Map **************

    let lat = locationManager.location!.coordinate.latitude
    let lon = locationManager.location!.coordinate.longitude


    let location = CLLocationCoordinate2D(latitude: lat, longitude: lon)
    let span = MKCoordinateSpanMake(0.70, 0.70)
    let region = MKCoordinateRegionMake(location, span)
    self.SpotterMap.setRegion(region, animated: true)

    self.EventMap.delegate = self;
    self.EventMap.showsUserLocation = true;


   print ("****** Welcome in MapViewController")


    let myGeoPoint = PFGeoPoint(latitude: lat, longitude:lon)

    let radius = 40.0

    print ("****** this is my geoPoint from map view controller: \(myGeoPoint)")


    //MARK: *** let's look for Events ***

    var nearArray : [CLLocationCoordinate2D] = []


        PFQuery(className: "EventInfo")
            .whereKey("location", nearGeoPoint: myGeoPoint, withinMiles: radius)     //(474)
            .findObjectsInBackgroundWithBlock ({
                objects, error in
                if let proximityArray = objects as? [PFObject] {
                    //                        println("****** here the proximity matches: \(proximityArray)")
                    for near in proximityArray {
                        //                            println("here they are \(near)")

                        let position = near["spotlocation"] as? PFGeoPoint

                        let locLat = position?.latitude       //this is an optional
                        let locLong = position?.longitude     //this is an optional
                        let SlocLocation = CLLocationCoordinate2D(latitude: locLat!, longitude: locLong!)

                        nearArray.append(SlocLocation)

                        if nearArray.isEmpty {
                            print ("*** ERROR! No Spots close by ***")
                        } else
                        {
                            for _ in nearArray {

                                let Start = near["starttime"] as! String
                                let End = near["endtime"] as! String
                                let Name = near["name"] as! String



                                let span = MKCoordinateSpanMake(0.40, 0.40)
                                let region = MKCoordinateRegionMake(SlocLocation, span)
                                self.EventMap.setRegion(region, animated: true)

                                // Sets the values of the annotation
                                let Annotation = CustomPointAnnotation()
                                Annotation.coordinate = SlocLocation
                                Annotation.title = Name
                                Annotation.subtitle = Start + " - " + End
                                Annotation.imageName = "Annotation.png"





                                self.EventMap.addAnnotation(Annotation)




                                let endTime = End

                                print(currentTimeString)

                                let TimeStr = endTime
                                formatter.timeStyle = .ShortStyle
                                let Time = formatter.dateFromString(TimeStr)

                                let dateFormater : NSDateFormatter = NSDateFormatter()
                                dateFormater.dateFormat = "h:mm ap"
                                let newTime = dateFormater.stringFromDate(Time!)
                                 print("\(currentTimeString) to \(newTime)")

                                if currentTimeString > newTime {



                                    self.EventMap.removeAnnotation(Annotation)
                                    print ("event removed")





                                }


                            }
                        }
                    }


                    }
                }

`

如果有人可以帮助我,那将是非常棒的!谢谢!

0 个答案:

没有答案