简体   繁体   中英

“Type of expression is ambiguous without more context” in NSURLSession

I am using Xcode 7 Beta 4, and when I am using NSURLSession, I get the error "Type of expression is ambiguous without more context". The code is as follows:

func updateData() {
    let url = NSURL(string: "http://www.stackoverflow.com")

    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
        let downloadedNews : NSData = data!
        let jsonError : NSError?

        do {
            let decodedJson = try NSJSONSerialization.JSONObjectWithData(downloadedNews, options: NSJSONReadingOptions.AllowFragments) as! Dictionary<String, AnyObject>
        } catch jsonError {
            print(jsonError)
        }
    }
    task.resume()
}

The error shows on the line where I declare the constant "task". This has been a problem for me for a few days and I have no idea how to fix it. Thank you so much for helping!

The error message is misleading, the actual problem is that catch takes a pattern and not a variable :

    } catch let jsonError {
        print(jsonError)
    }

(notice the additional let ). This also makes the local variable

    let jsonError : NSError?

obsolete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM