我通过在数组中运行循环来安排通知。通知工作正常。但我的应用程序崩溃了这个错误。我知道操作系统停止工作并且操作系统重新启动但是如果我不调用此scheduleNotification函数那么一切工作正常,所以我知道错误是生成因为这个功能,当我弹出到其他屏幕,然后又回来我的应用程序得到崩溃,但我不知道为什么?请帮帮我。
func scheduleNotification() {
if dictInfo.object(forKey: "name") !as AnyObject as ? String == "zzz" {
} else {
let calendar = Calendar.current
var calendarComponents = DateComponents()
let strDay = (dictInfo["dd"
as NSString] !as AnyObject).doubleValue
let strMonth = (dictInfo["mm"
as NSString]) !
let dayy = strDay
let monthh = strMonth
calendarComponents.hour = 11
calendarComponents.minute = 04
calendarComponents.day = Int(dayy!) - 2
print(calendarComponents.day)
let df = DateFormatter()
df.dateFormat = "MM" // if you need 3 letter month just use "LLL"
let datee1 = df.date(from: String(describing: monthh))
print(datee1)
let monthh11 = (Calendar.current as NSCalendar).component(.month, from: datee1!)
print(monthh11) // 5
//calendarComponents.year = Int(yearr!)
calendarComponents.month = Int(monthh11)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: calendarComponents.month, day: calendarComponents.day, hour: calendarComponents.hour, minute: calendarComponents.minute)
if# available(iOS 10.0, * ) {
let strName: String = String(describing: dictInfo["name"] !)
let str2: String! = "Today is \(strName)\'s Birthday!"
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Alert Me!"
content.body = str2
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "myCategory"
content.userInfo = ((dictInfo as Any) as ? [AnyHashable: Any]) !
print(content.userInfo.count)
let request = UNNotificationRequest(identifier: strName, content: content, trigger: trigger)
// UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {
(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
} else {
}
}
}
}
MBProgressHUD.hide(
for: self.view, animated: true)
tableVieww.isHidden = false
//self.navigationItem.leftBarButtonItem?.isEnabled = true
}