JSON Swift 2.0 中的解析错误
JSON parsing error in Swift 2.0
我正在学习在 Swift 2.0 中编写代码,但在将其编译到模拟器中时遇到了困难。 self.setLabels(data!) 行显示信息 线程 1:EXC_BAD_INSTRUCTION。谁能帮我这个?我正在尝试试错法,但还没有成功...
lass ViewController: UIViewController {
@IBOutlet weak var cityNameTextField: UITextField!
@IBOutlet weak var cityNameLabel: UILabel!
@IBOutlet weak var cityTempLabel: UILabel!
@IBAction func getWeatherDataClick(sender: AnyObject) {
getWeatherData("http://api.openweathermap.org/data/2.5/weather?q=" + cityNameTextField.text! + "")
}
override func viewDidLoad() {
super.viewDidLoad()
getWeatherData("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0")
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getWeatherData(urlString: String) {
let url = NSURL(string: urlString)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in
dispatch_async(dispatch_get_main_queue(), {
self.setLabels(data!)
})
}
task.resume()
}
func setLabels(weatherData: NSData) {
let jsonResult = AnyObject? ()
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(weatherData, options: []) as? NSDictionary {
print(jsonResult)
}
} catch {
print(error)
}
if let name = jsonResult!["name"] as? String {
cityNameLabel.text = name
}
if let main = jsonResult!["main"] as? NSDictionary {
if let temp = main["temp"] as? Double {
cityTempLabel.text = String(format: "%.1f", temp)
}
}
};
}
第一个猜测是:data == nil
。您的函数:setLabels:
未准备好接收 nil
参数。尝试将此函数的声明更改为:
func setLabels(weatherData: NSData?)
或者在你的 NSURLSession
块中调用 setLabels
之前更好地处理 data == nil
可能性:
if let weatherData = data as? NSData {
//your data is not nil
//you can securely call setLabels
self.setLabels(weatherData)
} else {
//ooops sth goes wrong your data is nil, try to figure out why
}
我正在学习在 Swift 2.0 中编写代码,但在将其编译到模拟器中时遇到了困难。 self.setLabels(data!) 行显示信息 线程 1:EXC_BAD_INSTRUCTION。谁能帮我这个?我正在尝试试错法,但还没有成功...
lass ViewController: UIViewController {
@IBOutlet weak var cityNameTextField: UITextField!
@IBOutlet weak var cityNameLabel: UILabel!
@IBOutlet weak var cityTempLabel: UILabel!
@IBAction func getWeatherDataClick(sender: AnyObject) {
getWeatherData("http://api.openweathermap.org/data/2.5/weather?q=" + cityNameTextField.text! + "")
}
override func viewDidLoad() {
super.viewDidLoad()
getWeatherData("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0")
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getWeatherData(urlString: String) {
let url = NSURL(string: urlString)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in
dispatch_async(dispatch_get_main_queue(), {
self.setLabels(data!)
})
}
task.resume()
}
func setLabels(weatherData: NSData) {
let jsonResult = AnyObject? ()
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(weatherData, options: []) as? NSDictionary {
print(jsonResult)
}
} catch {
print(error)
}
if let name = jsonResult!["name"] as? String {
cityNameLabel.text = name
}
if let main = jsonResult!["main"] as? NSDictionary {
if let temp = main["temp"] as? Double {
cityTempLabel.text = String(format: "%.1f", temp)
}
}
};
}
第一个猜测是:data == nil
。您的函数:setLabels:
未准备好接收 nil
参数。尝试将此函数的声明更改为:
func setLabels(weatherData: NSData?)
或者在你的 NSURLSession
块中调用 setLabels
之前更好地处理 data == nil
可能性:
if let weatherData = data as? NSData {
//your data is not nil
//you can securely call setLabels
self.setLabels(weatherData)
} else {
//ooops sth goes wrong your data is nil, try to figure out why
}