Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 我需要在每周的某一天发出通知。。但是我不在乎一个月的哪一天_Swift_Xcode_Swift2_Ios9_Xcode7 - Fatal编程技术网

Swift 我需要在每周的某一天发出通知。。但是我不在乎一个月的哪一天

Swift 我需要在每周的某一天发出通知。。但是我不在乎一个月的哪一天,swift,xcode,swift2,ios9,xcode7,Swift,Xcode,Swift2,Ios9,Xcode7,我的问题与我在下面链接的问题相同,但我想在swift 2中这样做,而不是在objective-c中 问题是,默认情况下,我使用的日期是1月1日,因为我没有指定月份、月份和年份,但如果我指定了它们,它将毫无用处,因为我的应用程序明年或下个月将无法工作 我想要的很简单。。在周日设置每周通知(例如) 但是指定年份或月份是不实际的,因为它应该在任何星期天或任何星期六工作 请帮帮我:(我已经试了很多了 let calendar = NSCalendar.currentCalendar() let com

我的问题与我在下面链接的问题相同,但我想在swift 2中这样做,而不是在objective-c中

问题是,默认情况下,我使用的日期是1月1日,因为我没有指定月份、月份和年份,但如果我指定了它们,它将毫无用处,因为我的应用程序明年或下个月将无法工作

我想要的很简单。。在周日设置每周通知(例如)

但是指定年份或月份是不实际的,因为它应该在任何星期天或任何星期六工作

请帮帮我:(我已经试了很多了

let calendar = NSCalendar.currentCalendar()
let components = NSDateComponents()


components.hour = 2
components.minute = 54
components.weekday = 6

var newDate = calendar.dateFromComponents(components)
let notification = UILocalNotification()
notification.fireDate = newDate
notification.alertBody = "Swipe to unlock"
notification.alertAction = "You've got a class soon!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["CustomField1": "w00t"]
notification.timeZone = NSTimeZone.localTimeZone()
notification.repeatInterval = NSCalendarUnit.WeekOfYear


UIApplication.sharedApplication().scheduleLocalNotification(notification)
更新:

我知道它只在星期一起作用,但我可以在以后修复。 这里的问题是,我将小时和分钟设置为0,然后它返回当前时间,我不知道如何设置我想要的小时

var pickerDate = NSDate()
print(pickerDate)

var dateComponents: NSDateComponents? = nil
var calendar = NSCalendar.currentCalendar()


dateComponents = calendar.components(NSCalendarUnit.NSWeekdayCalendarUnit, fromDate: pickerDate)
var firstMondayOrdinal = 9 - dateComponents!.weekday
dateComponents =  NSDateComponents()
dateComponents!.day = firstMondayOrdinal
dateComponents?.hour = 0
dateComponents?.minute = 0
var firstMondayDate = calendar.dateByAddingComponents(dateComponents!, toDate: pickerDate, options: NSCalendarOptions(rawValue: 0))

dateComponents = NSDateComponents()
dateComponents?.weekdayOrdinal = 1



let notification = UILocalNotification()
notification.fireDate = firstMondayDate
notification.alertBody = "Swipe to unlock"
notification.alertAction = "You've got a class soon!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["CustomField1": "w00t"]
notification.timeZone = NSTimeZone.localTimeZone()
notification.repeatInterval = NSCalendarUnit.WeekOfYear


UIApplication.sharedApplication().scheduleLocalNotification(notification)

获取下一个星期日或星期一的日期(),使用日历方法dateBySettingHour将所需时间设置为该日期,并使用weekofyear重复间隔将其用作firedate。

您的问题是如何在特定时间获取下一个星期日来设置firedate?嗯,我想在每周的特定日期发出通知,例如:我想发出通知每个星期一上午8点。我可以使用日期的组件来完成此操作,但它只在今天起作用,如果有人明年打开我的应用程序,则火灾日期将是过去的,您不理解。获取下一个星期日或星期一日期,使用日历方法dateBySettingHour将所需时间设置为该日期,并使用weekofyear repe将其用作火灾日期intervalI做到了,谢谢:)