0

I have the following function that requests reminder permission from the user:

func requestAccess(alertWith viewController: UIViewController) -> Bool {

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

            // check wether the user granted permission
            if granted {
                // FIXME: return true
            } else {
                // alert the user they didn't grant permission
                error?.alert(with: viewController, error: .permissionFailed(error!))
                // FIXME: return false
            }

        }

    }

It should return the 2 bools at the FIXMEs, but using plain return doesn't work (returning non-void from void method).

bcye
  • 758
  • 5
  • 22
  • 2
    Make your `requestAccess` method return void (nothing) then use the lines you have marked `FIXME:` to call other methods. You cannot use an async call to return a value in the enclosing method. Alternatively, use a completion handler. The duplicate link has solutions for this. – Chris Jul 01 '18 at 18:44
  • @Chris can I make requestAccess somehow also async and therefore return? – bcye Jul 01 '18 at 18:47
  • 1
    Yes that’s sort of what a completion handler does. You modify `requestAccess()` so it accepts a `()->Void` closure as an argument called `completionHandler`. Then you call `completionHandler()` in the `FIXME` lines. – Chris Jul 01 '18 at 18:51
  • 1
    Perfect, thanks! Forgot about that. – bcye Jul 01 '18 at 18:55
  • This is all covered in the duplicate I linked earlier. – rmaddy Jul 01 '18 at 20:07

0 Answers0