Swift throw from closure nested in a function

前端 未结 5 1391
醉话见心
醉话见心 2020-12-30 21:15

I have a function that throws an error, in this function I have a inside a closure that I need to throw the error from it\'s completion handler. Is that possibl

5条回答
  •  借酒劲吻你
    2020-12-30 21:40

    That's not possible in this case - that completion handler would have to be declared with throws (and the method with rethrows) and this one is not.

    Note that all that throwing is just a different notations for NSError ** in Objective-C (inout error parameter). The Objective-C callback doesn't have an inout parameter so there is no way to pass the error up.

    You will have to use a different method to handle errors.

    In general, NSError ** in Obj-C or throws in Swift don't play well with asynchronous methods because the error handling works synchronously.

提交回复
热议问题