EventKit

What am I doing Wrong??????

it says unexpected nil found while unwrapping 😟


yes there is a default reminder list set

and an account


  override func viewDidLoad() {
        super.viewDidLoad()
        eventStore = EKEventStore()
  
      
        eventStore.requestAccess(to: .reminder) { (granted, error) in
            if granted{
                allReminders = [EKReminder]()
                let cal = eventStore.calendars(for: .reminder).first
                let predicate = eventStore.predicateForIncompleteReminders(withDueDateStarting: nil, ending: nil, calendars: [cal!])
             
                eventStore.fetchReminders(matching: predicate, completion: { (reminders: [EKReminder]?) -> Void in
                    allReminders = reminders!
                })
              
  
            } else {
              
            }
        }
      
      
    }

Replies

Which line does it say that ?


Probably line 10, because of cal!


Look:


eventStore = EKEventStore()

object initialized, but empty.


let cal = eventStore.calendars(for: .reminder).first


this is nil, because there is no event yet. Hence collection is empty. Hence, first is nil.


let predicate = eventStore.predicateForIncompleteReminders(withDueDateStarting: nil, ending: nil, calendars: [cal!])


Boom ! cal! crashes


To correct :


You need also to have set a a key NSRemindersUsageDescription in info.plist

it says line 9 is nil

and there is a default list set

So it crashes on line 10 ?


In fact, there are usually reminders.


However, for double security, you could write:


               if let cal = eventStore.calendars(for: .reminder).first {
                    let predicate = eventStore.predicateForIncompleteReminders(withDueDateStarting: nil, ending: nil, calendars: [cal])
                   
                    eventStore.fetchReminders(matching: predicate, completion: { (reminders: [EKReminder]?) -> Void in
                         allReminders = reminders!
                    }
                    )
               }

Of course, essential to have set the key NSRemindersUsageDescription in info.plist with a String value.

the issue is not that it is crashing the issue is that it's not getting the default reminders list I have set a list but for some reason, it isn't working, ben trying to figure this out for abount 4 weeks I have even contacted apple

Code-Level Support

Thanks For the help anyway

So why did you write at first :


it says unexpected nil found while unwrapping

because it should not say nil if I set a default list

and I tried

let cal = EventStore.DefaultCalendarForNewReminders


it worked for the ios version of this app and now I am trying to make a mac app but I can't figure this out

Didn't you mispell with the uppercase ? And need to call the func result.


According to doc, it is logically:

let cal = eventStore.defaultCalendarForNewReminders()

Thanks For the help but i think i am just going to give up on the mac version of the app

You'll do it later ! And soon, IOS apps should run on MAc as well.


So, good continuation, and don't forget to mark this thread as closed.