Xcode 如何将应用程序中的本地通知时间设置为两次之间的随机时间?(swift)

Xcode 如何将应用程序中的本地通知时间设置为两次之间的随机时间?(swift),xcode,swift,xcode6,uilocalnotification,Xcode,Swift,Xcode6,Uilocalnotification,如何使用swift将应用程序中的本地通知时间设置为两次之间的随机时间? 例如上午8点到晚上10点之间 我在下面的代码中只指定了一个特定的时间(上午12点),但我不知道如何将时间“随机”设置为两次之间: import UIKit class ViewController: UIViewController { @IBOutlet weak var midnightSwitch: UISwitch! // you should declare your vars here l

如何使用swift将应用程序中的本地通知时间设置为两次之间的随机时间?
例如上午8点到晚上10点之间
我在下面的代码中只指定了一个特定的时间(上午12点),但我不知道如何将时间“随机”设置为两次之间:

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var midnightSwitch: UISwitch!
    // you should declare your vars here
    let localNotificationDailyAt12am = UILocalNotification()
    // you should declare your funcions here
    // you have to register the user notifications settings options (badge, alert and sound if desired)
    func setUIUserNotificationOptions() {
        UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil))
    }
    // now lets setup your notification
    func notificationsOptions12am() {
        // defines localtime
        localNotificationDailyAt12am.timeZone = NSTimeZone.localTimeZone()
        // confirms the alert dialog box action
        localNotificationDailyAt12am.hasAction = true
        // defines the notification alert body text
        localNotificationDailyAt12am.alertBody = "The midnight notification message"
        // defines the daily time interval
        localNotificationDailyAt12am.repeatCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
        localNotificationDailyAt12am.repeatInterval = .CalendarUnitDay
        // defines the notification alert button text
        localNotificationDailyAt12am.alertAction = "Open App"
        // defines the default sound for your notification
        localNotificationDailyAt12am.soundName = UILocalNotificationDefaultSoundName
        // increments the badge counter
        localNotificationDailyAt12am.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
        // defines your firedate tomorrow at 12am ( note: extension NSDate tomorrowAt12am at the bottom )
        localNotificationDailyAt12am.fireDate = NSDate().tomorrowAt12am
        // lets set it up
        UIApplication.sharedApplication().scheduleLocalNotification(localNotificationDailyAt12am)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setUIUserNotificationOptions()
        notificationsOptions12am()

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func switched(sender: AnyObject) {
        localNotificationDailyAt12am.fireDate = midnightSwitch.on ?
            NSDate().tomorrowAt12am :                                // tomorrow at 12am
                NSDate().dateByAddingTimeInterval(3155673600)        // will never fire (100 years from now)
    }
}
extension NSDate {
    var day:   Int { return NSCalendar.currentCalendar().components(.CalendarUnitDay,   fromDate: self).day   }
    var month: Int { return NSCalendar.currentCalendar().components(.CalendarUnitMonth, fromDate: self).month }
    var year:  Int { return NSCalendar.currentCalendar().components(.CalendarUnitYear,  fromDate: self).year  }
    var tomorrowAt12am: NSDate {
        return  NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day+1, hour: 0, minute: 0, second: 0, nanosecond: 0)!
    }

thx:)

您可以使用以下内容:

extension NSDate {
    class func randomTimeBetweenDates(lhs: NSDate, _ rhs: NSDate) -> NSDate {
        let lhsInterval = lhs.timeIntervalSince1970
        let rhsInterval = rhs.timeIntervalSince1970
        let difference = fabs(rhsInterval - lhsInterval)
        let randomOffset = arc4random_uniform(UInt32(difference))
        let minimum = min(lhsInterval, rhsInterval)
        let randomInterval = minimum + NSTimeInterval(randomOffset)
        return NSDate(timeIntervalSince1970: randomInterval)
    }
}
然后,安排两次约会:

let midnight = ...
let eightAm = ...
let random = NSDate.randomTimeBetweenDates(midnight, eightAm)

我在10:00到10:05之间尝试,但通知总是在10:04到25秒之间显示。这是代码:
let date:NSDate=NSDate()let cal=NSCalendar(calendarIdentifier:NSCalendarIdentifierGregorian)let midnight:NSDate?=cal?.dateBySettingHour(10,分钟:0,秒:0,of date:date,选项:NSCalendarOptions())让eightAm:NSDate?=cal?.dateBySettingHour(10,分钟:5,秒:0,of date:date,选项:NSCalendarOptions())让random=NSDate.RandomTimeBetween(午夜!八点!)
@Logan及以上:
localNotificationDailyAt12am.fireDate=random//让我们设置UIApplication.sharedApplication().scheduleLocalNotification()(localNotificationDailyAt12am)}@IBAction func switched(发送方:AnyObject){localNotificationDailyAt12am.fireDate=middnightswitch.on?random://明天上午12点NSDate().dateByAddingTimeInterval(3155673600)//永远不会触发(100年后)}您上面发布的代码对我有效,我得到了各种地址。(注释1)。您的代码中还有其他内容。我不知道问题出在哪里:s:s