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
如何在SwiftUI中将日期转换为日期组件?_Swift_Datepicker_Notifications_Uikit_Swiftui - Fatal编程技术网

如何在SwiftUI中将日期转换为日期组件?

如何在SwiftUI中将日期转换为日期组件?,swift,datepicker,notifications,uikit,swiftui,Swift,Datepicker,Notifications,Uikit,Swiftui,我想在用户从日期选择器中选择时设置每日通知,但我不知道如何将所选的小时和分钟转换为日期组件以匹配UNCalendarNotificationTrigger 这是我的日期选择器: DatePicker("Choose time of notif", selection: $notifTime, displayedComponents: .hourAndMinute) 这是我的通知触发器: var dateComponents = DateComponents() dateCo

我想在用户从日期选择器中选择时设置每日通知,但我不知道如何将所选的小时和分钟转换为日期组件以匹配UNCalendarNotificationTrigger

这是我的日期选择器:

DatePicker("Choose time of notif", selection: $notifTime, displayedComponents: .hourAndMinute)
这是我的通知触发器:

var dateComponents = DateComponents()
dateComponents = notifTime
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

您只需要使用
Calendar
API从
date
属性获取
dateComponents
,需要重复。因此,在这种情况下,小时和分钟每天都在重复,下面是您的操作方法:

var notifTime: Date
let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: notifTime)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)