ios - "Call can throw, but it is not marked with ' 通过将我的项目转换为 swift 2 来尝试 ' and the error is not handled"

标签 ios swift swift2

<分区>

我尝试将我的项目转换为 Swift 2,但遇到以下错误

Call can throw, but it is not marked with 'try' and the error is not handled

这是我的代码:

 override func viewDidLoad() {

     // Do any additional setup after loading the view.
     super.viewDidLoad()

     let query = PFUser.query()
     query!.whereKey("username", equalTo: username)

     var userArray = query!.findObjects() as! [PFUser]
     user = userArray[0]
    }

错误在以下几行:

 var userArray = query!.findObjects() as! [PFUser]
 user = userArray[0]

你能帮帮我吗?非常感谢!

最佳答案

让代码编译和运行的最简单方法是在违规行的开头添加 try!

但是,这是不安全的,就像您使用强制展开一样。

我建议您阅读 Swift Error Handling关于如何处理响应错误情况和从错误情况中恢复的更多信息。

您还应该阅读一些关于 Optionals 的资料。我建议你从 language reference 开始

关于ios - "Call can throw, but it is not marked with ' 通过将我的项目转换为 swift 2 来尝试 ' and the error is not handled",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34994699/

相关文章:

objective-c - 如何在一个 iOS 应用程序中列出所有现有的 iOS 应用程序以及从那里下载的选项

objective-c - 如何使用 ASIHTTPRequest 从 UIWebView 下载文件?

ios - 启动AdHoc应用程序时,“进程启动失败:尝试启动应用程序超时”

swift - 我怎样才能让两个球员在触球时都移动?

swift - 发送带参数的 Alamofire 请求以及 HTTP 正文中的图像

iphone - 在 iOS 中存储日常数据

ios - prepareForSegue 不能快速工作。我该如何解决?

ios - swift xcode 播放播放器列表中的声音文件

ios - 如何在 Swift 中清除互联网数据?

swift2 - swift 2 : Parse RSS pubDate to NSDate object