Swift 2:从类型的抛出函数到非抛出函数的无效转换 [英] Swift 2: Invalid conversion from throwing function of type to non-throwing function

查看:101
本文介绍了Swift 2:从类型的抛出函数到非抛出函数的无效转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些(难看的)自写代码移植到Swift2,并在lambda函数中得到了此错误消息:

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

我不了解的是,我使用错误抛出函数JSONObjectWithData处理整个代码并捕获错误.我没有在代码中扔任何东西.尽管如此,编译器意味着我抛出了一个错误.

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.

我需要了解这种行为.请保持友好,因为我知道我必须改进代码以充分利用swift2中的新错误处理概念.

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.

非常感谢您.

推荐答案

这很快.借助本文的一些帮助,我已经找到解决问题的方法:

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

您必须在代码末尾放置一个通用catch子句,因为仅NSError的捕获是不够的.

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
{
}

这篇关于Swift 2:从类型的抛出函数到非抛出函数的无效转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆