2015-02-08 102 views
0

我需要安排通知。如果從具有dateFromData時間組件的當前日期日期組件創建的日期早於當前日期,那麼我想將其更改爲第二天。這是我到目前爲止。無論我如何設置,日期比較劑量都不起作用。它要麼總是改變它,要麼永遠不會改變它。Swift NSDate比較

var dateComps = calendar.components(NSCalendarUnit.YearCalendarUnit | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.DayCalendarUnit | NSCalendarUnit.HourCalendarUnit | NSCalendarUnit.MinuteCalendarUnit, fromDate: NSDate()) 

dateComps.hour = calendar.component(NSCalendarUnit.HourCalendarUnit, fromDate: dateFromData) 
dateComps.minute = calendar.component(NSCalendarUnit.MinuteCalendarUnit, fromDate: dateFromData) 


    if fireDate.compare(NSDate()) == NSComparisonResult.OrderedDescending { 
     //change day to next day 

     dateComps.day += 1 
     println("Change day") 
    }else{ 
     println("Do not change day") 
    } 




    let notifactionOfAmountOfWork = UILocalNotification() 
     notifactionOfAmountOfWork.category = "normalNotifactionCatagory" 

    notifactionOfAmountOfWork.fireDate = calendar.dateFromComponents(dateComps) 

回答

0

您firstDate定義如何?下面的程序有效,我只是試了一下。你可以改變comps1.day和comps2.day來讓它碰到這兩個條件。

import UIKit 

let calendar1 = NSCalendar.currentCalendar() 
let comps1 = NSDateComponents() 
comps1.day = 7 
let date1 = calendar1.dateByAddingComponents(comps1, toDate: NSDate(), options: NSCalendarOptions.allZeros) 

let calendar2 = NSCalendar.currentCalendar() 
let comps2 = NSDateComponents() 
comps2.day = 0 
let date2 = calendar2.dateByAddingComponents(comps2, toDate: NSDate(), options: NSCalendarOptions.allZeros) 


if date1?.compare(date2!) == NSComparisonResult.OrderedDescending 
{ 
    println("date1 is after date 2") 
}else{ 
    println("date1 is before date 2") 
} 
+1

謝謝,問題是,dateFromData是從時間日期選擇器視圖。 – 2015-02-10 03:31:15