我每天都会收到通知,提醒用户使用该应用。我希望文本不是静态的。我已经说过,5个不同的字符串,如果可能的话,我希望随机选择字符串。
我目前的代码,每天使用相同的文字:
let date: NSDate = NSDate()
let cal: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
let newDate: NSDate = cal.dateBySettingHour(22, minute: 00, second: 0, ofDate: date, options: NSCalendarOptions())!
var localNotification: UILocalNotification = UILocalNotification()
localNotification.alertAction = "MyApp"
localNotification.alertBody = "Call to action text"
localNotification.fireDate = newDate
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
答案 0 :(得分:1)
您可以根据星期几使用switch
语句。这是excellent NSHipster tutorial on NSDate components。这里有一些sample code:如何从NSDate中抽出一周的一天:
func getDayOfWeek(today:String)->Int {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let todayDate = formatter.dateFromString(today)!
let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let myComponents = myCalendar.components(.CalendarUnitWeekday, fromDate: todayDate)
let weekDay = myComponents.weekday
return weekDay
}
let weekday = getDayOfWeek("2014-08-27")
println(weekday) // 4 = Wednesday
对于switch
语句,
基本上,您希望创建一种方法,将NSDate
作为输入并吐出一周中的某一天。一旦您获得了一周中的某一天,就可以在switch
声明中创建自定义消息。