j-s
j-s

Reputation: 157

Cannot convert value of type '(Bool, NSError?) -> Void'

I am building a reminder app. And I am trying to implement the required code to fetch all the reminders saved in the Calendar database.

Following is a part of my code:

override func viewWillAppear(_ animated: Bool) {
    // 1
    self.eventStore = EKEventStore()
    self.reminders = [EKReminder]()
    self.eventStore.requestAccessToEntityType(EKEntityType.Reminder) {(granted: Bool, error: NSError?) -> Void in
        if granted{
            // 2
            let predicate = self.eventStore.predicateForRemindersInCalendars(nil)
            self.eventStore.fetchRemindersMatchingPredicate(predicate, completion: { (reminders: [EKReminder]?) -> Void in

                self.reminders = reminders
                dispatch_async(dispatch_get_main_queue()) {
                    self.tableView.reloadData()
                }
            })
        }else{
            print("The app is not permitted to access reminders, make sure to grant permission in the settings and try again")
        }
    } as! EKEventStoreRequestAccessCompletionHandler as! EKEventStoreRequestAccessCompletionHandler
}

I keep getting an error in this part of the code:

self.eventStore.requestAccessToEntityType(EKEntityType.Reminder) {(granted: Bool, error: NSError?) -> Void in

The error is:

Cannot convert value of type '(Bool, NSError?) -> Void' to expected argument type 'EKEventStoreRequestAccessCompletionHandler' (aka '(Bool, Optional) -> ()')

This is on swift 4.0. Any clue on how this could be fixed? I tried all the solutions available but couldn't find a proper fix.

Upvotes: 1

Views: 667

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100503

It seems you use an older version of swift may be swift 2 , it should be

self.eventStore.requestAccessToEntityType(to: EKEntityType.reminder) { (granted, error) in }

//

In swift 4

self.eventStore.requestAccess(to: EKEntityType.reminder) { (granted, error) in }

Upvotes: 1

Ivan Smetanin
Ivan Smetanin

Reputation: 2039

Looks like that your code wrote in Swift 2

You can try to not use parameter types in closure:

self.eventStore.requestAccess(to: EKEntityType.reminder) { (granted, error) in
    ...
}

And you shouldn't use these force casts:

as! EKEventStoreRequestAccessCompletionHandler as! EKEventStoreRequestAccessCompletionHandler`

Complete Swift 4 code:

override func viewWillAppear(_ animated: Bool) {
    self.eventStore = EKEventStore()
    self.reminders = [EKReminder]()
    self.eventStore.requestAccess(to: EKEntityType.reminder) { (granted, error) in
        if granted{
            let predicate = self.eventStore.predicateForReminders(in: nil)
            self.eventStore.fetchReminders(matching: predicate, completion: { reminders in
                guard let reminders = reminders else {
                    return
                }
                self.reminders = reminders
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            })
        } else {
            print("The app is not permitted to access reminders, make sure to grant permission in the settings and try again")
        }
    }
}

Upvotes: 2

Related Questions