I have very simple class which fetches the contacts. Now I need to create this function with throws.
As store.requestAccess
is not throwing function so I can't throw any error from that clousure.
So I am getting this error
Invalid conversion from throwing function of type '(_, _) throws -> ()' to non-throwing function type '(Bool, Error?) -> Void'
class ContactFetcher {
enum ContactError:Error {
case permissionError
case fetchError
}
func fetchContacts(completion:@escaping(([CNContact]) -> ())) throws {
let keys = [CNContactPhoneNumbersKey] as [CNKeyDescriptor]
let fetchRequest = CNContactFetchRequest(keysToFetch: keys)
let store = CNContactStore()
var results:[CNContact] = []
store.requestAccess(for: .contacts) { (grant, error) in
if grant{
do {
try store.enumerateContacts(with: fetchRequest, usingBlock: { (contact, stop) -> Void in
results.append(contact)
})
} catch {
// throw ContactError.fetchError
}
completion(results)
} else {
throw ContactError.permissionError
print("Error \(error)")
}
}
}
}
Is there any way to fix this ?
Thanks in advance