我是新来的iOS发展。我正在尝试加载一个JSON,这是我的功能:
func loadmyJSON (urlPath: String) {
let url: NSURL = NSURL(string: urlPath)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
println("Task completed")
if(error != nil) {
// If there is an error in the web request, print it to the console
println("error not nil::::::")
println(error.localizedDescription)
}
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
if(err != nil) {
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
}
let results: NSArray = jsonResult["variants"] as NSArray
dispatch_async(dispatch_get_main_queue(), {
self.jsonTable = results
self.productView!.reloadData()
})
})
但我收到此错误:
error not nil::::::
The operation couldn’t be completed. (NSURLErrorDomain error -1002.)
我可以通过相同的 URL 在浏览器中获取 JSON。
我尝试过阅读这个苹果文档 https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html,但我无法得到描述。
这个错误代码是什么意思?
NSURLErrorDomain error -1002
means NSURLErrorUnsupportedURL
or kCFURLErrorUnsupportedURL
。它表明提供了错误的 URL,在大多数情况下,丢失了http://
来自 URL 前缀。
全部名单NSURLErrorDomain
代码可以在这里找到:https://developer.apple.com/documentation/foundation/1508628-url_loading_system_error_codes https://developer.apple.com/documentation/foundation/1508628-url_loading_system_error_codes
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)