2016-02-03 116 views
0
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) 
     } 

回答

0

我找到了解决方案,因为我试图通过http/https获取数据,它会打开一个漏洞并引发此安全异常,所以我在处理它的我的info.plist文件中添加了以下行,

<key>NSAppTransportSecurity</key><dict><key>NSAllowsArbitraryLoads</key><true/></dict> 
0

这可能是你不正确地解开你的JSON数据。如果有错误输入,可能会在解包时找到零。如果让我们解析一下你的JSON或者某种类型的JSON解析器,你会使用它吗?你介意发布完整的setLabels()函数吗?

+0

我添加的代码,你可以请大家看看,谢谢 – Natto

相关问题