【发布时间】:2014-10-30 07:37:01
【问题描述】:
我是 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,但无法获得描述。
这个错误代码是什么意思?
【问题讨论】:
-
-1002是NSURLErrorUnsupportedURL,说明你的
urlPath是错误的。 -
NSURLErrorDomain 错误 -1002 表示
NSURLErrorUnsupportedURL或kCFURLErrorUnsupportedURL。您的 url 字符串很可能在 url 前缀中缺少“http://”。 -
@AsifAsif : 但是你从哪里得到这些错误代码的描述呢?
-
@sasquatch:你可以在这里找到列表(将
Language切换到Obj-C):developer.apple.com/library/mac/documentation/Cocoa/Reference/…