I am trying to create dateComponents from Date stored in CoreData from UIDatePicker. However, I am not able to make it. I am first converting Date which is stored in editnotes?.sSelectedDate
into dateComponents
and then using the dateComponents for triggering my notifications.
Here is what I am doing:
var date = Date()
if let editnote = editnotes
{
date = editnote.sSelectedDate
}
else
{
print("nil")
}
let calendar = NSCalendar.current
let unitFlags = Set<Calendar.Component>([.day, .month, .year, .hour, .minute])
let anchorComponents = calendar.dateComponents(unitFlags, from: date)
var dateInfo1 = DateComponents()
dateInfo1.day = anchorComponents.day
dateInfo1.month = anchorComponents.month
dateInfo1.hour = anchorComponents.hour
dateInfo1.minute = anchorComponents.minute
dateInfo1.year = anchorComponents.year
let trigger = UNCalendarNotificationTrigger(dateMatching:dateInfo1, repeats: false)
I am doing this:
if let editnote = editnotes
{
date = editnote.sSelectedDate
}
else
{
print("nil")
}
to unwrap the optional but it's not working still. Continue reading!
I know I can use anchorComponents but that wasn't working so I copied those into a new DataComponents instance.
I am trying to print the what is stored indateInfo1.hour
and I get
Optional(2)
I reckon this is the reason why dateComponents is not working for triggering notifications at scheduled time. Help will be appreciated?