【问题标题】:Throwing errors from closure从关闭中抛出错误
【发布时间】:2019-07-13 18:18:32
【问题描述】:

我的应用中有这段代码:

func saveContact2(contact: String) throws {
    let contactStore = CNContactStore()
    contactStore.requestAccess(for: .contacts, completionHandler: {(granted, error) in
        if granted && error == nil {
            //...
        } else {
            if !granted {
                throw contactErrors.contactAccessNotGranted(["Error","Access to Contacts is not granted."])
            }
        }
    })
}

我想将所有在调用函数关闭时引发的错误都抛出。

编译器显示错误:

从'(_, _) throws -> ()' 类型的抛出函数到非抛出函数类型'(Bool, Error?) -> Void'的无效转换

谁能帮我正确的语法?

【问题讨论】:

    标签: swift closures throw


    【解决方案1】:

    您不能从异步调用的@escaping 闭包中抛出错误。这是有道理的,因为您的应用程序已继续执行,并且没有地方可以捕获错误。

    因此,请自行采用完成处理程序模式:

    func saveContact2(_ contact: String, completion: @escaping: (Result<Bool, Error>) -> Void) {
        let contactStore = CNContactStore()
        contactStore.requestAccess(for: .contacts) { (granted, error) in
            guard granted else {
                completion(.failure(error!)
                return
            }
    
            //...
            completion(.success(true))
        }
    }
    

    然后你会这样称呼它:

    saveContact2(contactName) { result in 
        switch result {
        case .failure:
            // handler error here
    
        case .success:
            // handle confirmation of success here
        }
    }
    

    如果您使用的是没有Result 类型的旧编译器,则基本上是:

    enum Result<Success, Failure> where Failure: Error {
        case success(Success)
        case failure(Failure)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-12
      相关资源
      最近更新 更多