ios - NSURLErrorDomain 错误码1002说明

标签 ios json swift

我是 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。 我试过阅读 this Apple Doc ,但我无法获取描述。

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

最佳答案

NSURLErrorDomain 错误 -1002 表示 NSURLErrorUnsupportedURLkCFURLErrorUnsupportedURL。它表示提供了错误的 URL,在大多数情况下,URL 前缀中缺少 http://

可以在此处找到所有 NSURLErrorDomain 代码的列表:https://developer.apple.com/documentation/foundation/1508628-url_loading_system_error_codes

关于ios - NSURLErrorDomain 错误码1002说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26647423/

相关文章:

从 XIB 加载的 iOS UITableView header

Javascript 将对象插入特定索引

ios - 初始化 UIColor displayP3 颜色时的最佳实践

swift - 如何解决字符串大写问题

ios - iBeacons 发送和接收 UUID

ios - 谷歌登录后使用电子邮件登录Firebase出现错误: The password is invalid or the user does not have a password

objective-c - 如何对包含嵌套 JSON 字符串的 NSSArray 进行排序?

javascript - 使用 AJAX 和 JQuery 动态操作 JSON API 数据

swift - UnsafeMutablePointer 的奇怪行为

ios - 防止 UITextField 在更改 firstResponder 时取值