从这里抛出的Swift错误没有处理 [英] Swift Errors Thrown from here are not handled

查看:103
本文介绍了从这里抛出的Swift错误没有处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Xcode 6中使用了代码,但是由于我有Xcode 7,我无法弄清楚如何解决这个问题。 let jsonresult行有一个错误,表示从这里抛出的错误未被处理。代码如下:

  func connectionDidFinishLoading(connection:NSURLConnection!){

let jsonresult:NSDictionary =尝试NSJSONSerialization.JSONObjectWithData(self.bytes,options:NSJSONReadingOptions.MutableContainers)as! NSDictionary

print(jsonresult)

let number:Int = jsonresult [count] as! Int

print(number)

numberElements = number

让结果:NSDictionary = jsonresult [results] as! NSDictionary

let collection1:NSArray = results [collection1] as! NSArray

谢谢

解决方案

如果您查看swift 2中 JSONObjectWithData 方法的定义,则会抛出错误。

 类func JSONObjectWithData(data:NSData,options opt:NSJSONReadingOptions)throws  - > AnyObject 

在swift 2中,如果某些函数抛出错误,则必须用do-try-catch块



这是它的工作原理

  func connectionDidFinishLoading(connection: NSURLConnection!){
do {
let jsonresult:NSDictionary = try NSJSONSerialization.JSONObjectWithData(self.bytes,options:NSJSONReadingOptions.MutableContainers)as! NSDictionary
print(jsonresult)

let number:Int = jsonresult [count] as! Int

print(number)

numberElements = number

让结果:NSDictionary = jsonresult [results] as! NSDictionary

let collection1:NSArray = results [collection1] as! NSArray
} catch {
// handle error
}
}

或者如果您不想要处理错误,可以使用 try!关键字强制执行。

  let jsonresult:NSDictionary = try! NSJSONSerialization.JSONObjectWithData(self.bytes,options:NSJSONReadingOptions.MutableContainers)as! NSDictionary 
print(jsonresult)

与其他关键字结束!这是一个危险的操作。如果您的程序出现错误,您的程序将崩溃。


I have had my code working in Xcode 6 but since I got Xcode 7 I cannot figure out how to fix this. The let jsonresult line has an error that says Error thrown from here are not handled. Code is below:

func connectionDidFinishLoading(connection: NSURLConnection!) {

    let jsonresult:NSDictionary = try NSJSONSerialization.JSONObjectWithData(self.bytes, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary

    print(jsonresult)

    let number:Int = jsonresult["count"] as! Int

    print(number)

    numberElements = number

    let results: NSDictionary = jsonresult["results"] as! NSDictionary

    let collection1: NSArray = results["collection1"] as! NSArray

Thanks

解决方案

If you look at the definition of JSONObjectWithData method in swift 2 it throws error.

class func JSONObjectWithData(data: NSData, options opt: NSJSONReadingOptions) throws -> AnyObject

In swift 2 if some function throws an error you have to handle it with do-try-catch block

Here is how it works

func connectionDidFinishLoading(connection: NSURLConnection!) {
    do {
        let jsonresult:NSDictionary = try NSJSONSerialization.JSONObjectWithData(self.bytes, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
        print(jsonresult)

        let number:Int = jsonresult["count"] as! Int

        print(number)

        numberElements = number

        let results: NSDictionary = jsonresult["results"] as! NSDictionary

        let collection1: NSArray = results["collection1"] as! NSArray
    } catch {
        // handle error
    }
}

Or if you don't want handle error you can force it with try! keyword.

let jsonresult:NSDictionary = try! NSJSONSerialization.JSONObjectWithData(self.bytes, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
        print(jsonresult)

As with other keywords that ends ! this is a risky operation. If there is an error your program will crash.

这篇关于从这里抛出的Swift错误没有处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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