Swift 2: Invalid conversion from throwing function of type to non-throwing function

南楼画角 提交于 2019-12-19 05:08:41

问题


I have some (ugly) self-written code ported to Swift2 and got this error message in a lambda function:

What I didn't understand is, that I handle the whole code with the error throwing function JSONObjectWithData and catch the error. I throw nothing in the code. Nevertheless the compiler means that I am throwing an error.

I need to understand this behavior. Please be kind because I know that I have to improve my code to make full use of the new error handling concept in swift2.

Thank you very much in advance.


回答1:


This was fast. I have figured the solution for my problem out with a little help of this article:

http://www.hackingwithswift.com/new-syntax-swift-2-error-handling-try-catch

you have to put a general catch clause at the end of the code because the catch of NSError alone is not sufficient.

catch let error as NSError
{
   failure(error: error)
   return
}

// this is important -->
catch
{
}



回答2:


I think the best way forward is to change your failure function signature to take an ErrorType. Then just

catch let error {
    failure(error: error)
}

will do.



来源:https://stackoverflow.com/questions/30826560/swift-2-invalid-conversion-from-throwing-function-of-type-to-non-throwing-funct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!