我正在尝试修复每天显示的通知,我应该在第二天为学校收拾什么。
一切似乎都没问题,但content.subtitle
显示错误:
主题1:EXC_BREAKPOINT(代码= 1,子代码= 0x1007dbb00)
阵列也有问题吗?我搜索了答案,但看起来没有人使用数组通知机构。它甚至可能吗? 如果不是,我可以用更好的替代品替换它吗? 谢谢你的帮助。
let plan = [
["dzien": "Sunday", "lekcje": nil],
["dzien": "Saturday", "lekcje":["pon1","pon2","pon3"]],
["dzien": "Tuesday", "lekcje":["wt1","wt2","wt3"]],
["dzien": "Wednesday", "lekcje":["sr1","sr2","sr3"]],
["dzien": "Thursday", "lekcje":["cz1","cz2","cz3"]],
["dzien": "Friday", "lekcje":["pt1","pt2","pt3"]],
["dzien": "Saturday", "lekcje": nil],
]
func getDayOfWeek() -> Int {
let myCalendar = Calendar(identifier: .gregorian)
let weekDay = myCalendar.component(.weekday, from: Date())
return weekDay - 1
}
@objc func registerLocal() {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("Sukces")
} else {
print("Nie dzieła")
}
}
}
@objc func scheduleLocal() {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
let d = getDayOfWeek()%6
let dzis = plan[d]
if dzis["lekcje"] != nil {
content.title = "Pack up"
content.subtitle = dzis["dzien"]
var lekcje = ""
for lekcja in dzis["lekcje"] {
lekcje+="\(lekcja)\n"
}
content.body = lekcje
content.userInfo = ["customData": "fizzbuzz"]
} else {
content.title = "Tommorow \(dzis["dzien"]), You don't have any subjects."
}
var dateComponents = DateComponents()
dateComponents.hour = 16
dateComponents.minute = 00
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
}
}
答案 0 :(得分:0)
问题是编译器将plans
的类型推断为[String:Any]
,当您尝试content.subtitle = dzis["dzien"]
时,dzis["dzien"]
的类型为Any
而不是String
。
为了工作,您必须将其强制转换为String
。
guard let subtitle = dzis["dzien"] as? String else {return}
content.subtitle = subtitle