Ios Swfit 2“;额外参数';错误';“随时待命”;

Ios Swfit 2“;额外参数';错误';“随时待命”;,ios,swift,swift2,Ios,Swift,Swift2,当xcode给我它一直指向的“调用中的额外参数‘error’in call”时,我一直无法理解xcode想要我做什么 if let feed = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: nil) as? NSDictionary, 我在某个地方读到,在swift 2中,我应该添加do{,但每次我添加它时,我总是破坏更多的东西。swift 2中正确的语法是什么 代码如下: o

当xcode给我它一直指向的“调用中的额外参数‘error’in call”时,我一直无法理解xcode想要我做什么

if let feed = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: nil) as? NSDictionary,
我在某个地方读到,在swift 2中,我应该添加do{,但每次我添加它时,我总是破坏更多的东西。swift 2中正确的语法是什么

代码如下:

override func viewDidLoad() {
    super.viewDidLoad()
    let request = NSURLRequest(URL: NSURL(string: feedURL)!)
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { response, data, error in
        if let feed = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: nil) as? NSDictionary,
            title = feed.valueForKeyPath("feed.entry.im:name.label") as? String,
            artist = feed.valueForKeyPath("feed.entry.im:artist.label") as? String,
            imageURLs = feed.valueForKeyPath("feed.entry.im:image") as? [NSDictionary] {
                if let imageURL = imageURLs.last,
                    imageURLString = imageURL.valueForKeyPath("label") as? String {
                        self.loadImageFromURL(NSURL(string:imageURLString)!)
                }
            self.titleLabel.text = title
            self.titleLabel.hidden = false
            self.artistLabel.text = artist
            self.artistLabel.hidden = false

        }
    }
}

Swift 2中执行此操作的方法是

let feed = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers)

以下是swift 2中错误处理的新方法

do {
     if let feed = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as? NSDictionary {
      // Success block...
   }
} catch {
    print(error)
}

这是因为在Swift 2中,该方法没有错误参数。是的,但正确的方法是什么?可能重复我以前尝试过删除它一次,但我得到一个“调用可以抛出,但它没有标记为“try”,错误未得到处理”感谢您的帮助编译器将接受上述代码,而不会对此抱怨。如果您不使用
If
按上述方式执行,这将很好。@pbush25-现在,如果您不想使用
try
-
catch
语法,您可以使用
try?
try!
。如果让…
执行
则仅执行
是不够的cient.哦,是的,我以前见过,但是如何用旧的错误处理方法替换它而不破坏周围的一切?或者我应该重写整个内容吗?只需将代码从标题复制到
self.artistLabel.hidden=false
,并在上述条件下将其放入成功块即可。。。