override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func onbutton(sender: UIButton) {
inputarea.text = "Success"
getweatherdata("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98")
// UIApplication.sharedApplication().openURL(NSURL(string:"http://www.reddit.com/")!)
}
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()
}
我想实现一个简单的天气应用程序,我能够编译代码,但在运行后,一旦我面对错误“主题1: exc_bad_instruction(code = exc_i386_invop,subcode = 0x0)error“在函数调用self.setLabels(data!)时,我检查了url,它返回的数据不是零。线程1:exc_bad_instruction(代码= exc_i386_invop,子码=为0x0)错误
我具备的功能setLabels其提取儿子的数据并显示它,我会很乐意提供整个代码,如果有人需要it.Thanks
请找到下面setLabels功能,应用程序的名字和TEMPL是标签在视图
func setLabels(weatherdata: NSData){
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(weatherdata, options: []) as? NSDictionary {
print(jsonResult)
if let name = jsonResult["name"] as? String{
appname.text = name
}
if let main = jsonResult["main"] as? String{
//if let temp = main["temp"] as? Double {
//templ.text = String(format: "%.1f",temp)
templ.text = main
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
我添加的代码,你可以请大家看看,谢谢 – Natto