我是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()
})
})
但是我遇到了这个错误:
错误不为空::::::
操作无法完成。(NSURLErrorDomain错误-1002。)
我可以通过相同的URL在浏览器中获取JSON。 我尝试阅读这篇苹果文档,但我无法理解其描述。
这个错误代码的含义是什么?
urlPath
是错误的。 - Martin RNSURLErrorUnsupportedURL
或kCFURLErrorUnsupportedURL
。最有可能的是您的URL字符串缺少URL前缀中的“http://”。 - Asif Asif