Swfit 2“呼叫中的额外参数'错误'

时间:2015-11-02 03:56:56

标签: ios swift swift2

我一直在理解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

        }
    }
}

2 个答案:

答案 0 :(得分:2)

这是swift 2中新的错误处理方式......

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

答案 1 :(得分:0)

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

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