【发布时间】:2014-11-22 10:02:05
【问题描述】:
我正在尝试为我的 php/mysql 网站构建一个 IOS 应用程序。问题是我无法让以下代码与我创建的 JSON 输出一起使用。但相同的代码适用于其他 API,例如:
这是我的代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Playground - noun: a place where people can play
let urlPath = "https://www.example.com/API_OUT.php?API_KEY=785d...e5f5"
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.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)")
} else {
println(jsonResult)
}
})
task.resume()
}
}
【问题讨论】:
-
这看起来不错。您建议它不起作用,但不要准确地告诉我们它的作用。网络请求是否失败? JSON解析是否失败?这究竟是哪里失败了?它会报告什么错误(如果有)?
-
您好,xcode 控制台没有错误,但是应用程序崩溃并出现 EXC_BREAKPOINT (code=EXC_i386_BPT subcode=0x0)
-
请提供足够的信息:1)是否打印了“任务完成”? 2)您是否单步执行代码以查看它在哪一行崩溃? (另外,应该是
println(jsonResult.description)。 -
Yes Task Completed 已打印。它没有显示是哪一行导致了问题,但是当我用 telize.com/geoip 替换 URL 时它工作正常!!!!
-
你的应用在哪里崩溃????