【发布时间】:2021-07-28 03:10:47
【问题描述】:
我希望能够在每天的特定时间通知用户我的应用。在这个例子中,时间是中午
import SwiftUI
import UserNotifications
struct Alert: View {
@State var noon = false
func noonNotify() {
let content = UNMutableNotificationContent()
content.title = "Meds"
content.subtitle = "Take your meds"
content.sound = UNNotificationSound.default
var dateComponents = DateComponents()
dateComponents.hour = 14
dateComponents.minute = 38
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// choose a random identifier
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
// add our notification request
UNUserNotificationCenter.current().add(request)
}
var body: some View {
VStack {
Toggle(isOn: $noon) {
Text("ThirdHour")
}
if noon {
noonNotify()
}
Button("Request Permission") {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
print("All set!")
} else if let error = error {
print(error.localizedDescription)
}
}
}
}
}
}
我创建了一个函数,当切换为真时,函数将执行,但当它为假时,它不会执行。但是,当我创建一个 if 语句时,我得到一个错误
类型'()'不能符合'View';只有结构/枚举/类类型可以符合协议
谁能解释我做错了什么?
【问题讨论】: