-1

I've been really stuck on this one for a while. The user will set a custom alarm interval and then pick a date on the picker, and the app is set to display the next alarm. If they set it in the past, I want the app to add the interval by looping until it is in the future.

I've been playing around with the date comparisons but I can't seem to get anything to work. How would you guys accomplish this? Thanks!

4

1 回答 1

1

您可以添加此扩展程序以检查他们的日期是否在给定日期之前:

extension NSDate
{
    func isGreaterThanDate(dateToCompare : NSDate) -> Bool
    {
        //Declare Variables
        var isGreater = false

        //Compare Values
        if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
        {
           isGreater = true
        }

       //Return Result
        return isGreater
    }


    func isLessThanDate(dateToCompare : NSDate) -> Bool
    {
        //Declare Variables
        var isLess = false

       //Compare Values
       if self.compare(dateToCompare) == NSComparisonResult.OrderedAscending
        {
            isLess = true
        }

        //Return Result
        return isLess
    }
}

要使用它,只需执行类似的操作

let today = NSDate()//this creates a date object with current date
let isBeforeToday = today.isGreaterThanDate(aDate)//aDate is whatever date they pick from the picker

然后添加你的间隔你可以做

today.dateByAddingTimeInterval(customInterval)//customInterval is in seconds

我不确定我确切地知道你想做什么,但这至少应该让你开始比较日期并为它们添加间隔

于 2016-03-04T21:45:24.087 回答