NSURLErrorDomain错误代码1002说明

我是iOS开发新手。 我想加载一个JSON ,这是我的function:

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。 我试过看这个 :但是我不能得到描述。

这个错误代码是什么意思?

NSURLErrorDomain error -1002表示NSURLErrorUnsupportedURLkCFURLErrorUnsupportedURL 。 这表明提供了一个错误的URL,在大多数情况下,URL前缀中缺lesshttp://

所有NSURLErrorDomain代码的列表可以在这里find: https : //developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/index.html#//apple_ref/doc/constant_group/URL_Loading_System_Error_Codes