環境
swift2.0
xcode7
現在教材を参考にし、Newsサイトを作成しています。
以下で実行した場合、
Thread 6: EXC_BAD_INSTRUCTION(code=EXC_l386_INVOP.subcode=0x0)
のエラーが出ます。
プログラムは写経したもので、どこで何をしているかは少しは理解できますが、行き詰まりました。
パースしたのものサイズが大きく、オーバーフローしているためエラーが出ているのでしょうか?
do try を使用し、エラーハンドリングできていると思っていますが、実行時エラーが出ます。
初心者のため、そこが問題なのかも怪しいですが、
解決方法をご教授いただければ助かります。
swift
1import UIKit 2 3class ViewController: UITableViewController { 4 var entries = NSArray() 5 6 let newsUrlString = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://rss.itmedia.co.jp/rss/2.0/news_bursts.xml&num=8" 7 8 @IBAction func refresh(sender: AnyObject) { 9 let url = NSURL(string: newsUrlString)! 10 11 let task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { data,response, error in 12 13 do { 14 // JSONデータを辞書に変換する 15 let dict = try NSJSONSerialization.JSONObjectWithData(data!, 16 options: NSJSONReadingOptions.MutableContainers) as! NSDictionary 17 18 // /responseData/feed/entriesを取得する 19 guard let responseData = dict["responseData"] as? NSDictionary 20 else {return} 21 guard let feed = responseData["feed"] as? NSDictionary else {return} 22 guard let entries = feed["entries"] as? NSArray else {return} 23 self.entries = entries 24 } catch {} 25 // メインスレッドにスイッチする 26 dispatch_async(dispatch_get_main_queue(), { 27 // テーブルビューを更新する 28 self.tableView.reloadData() 29 }) //in complitionHandler 30 }) 31 task.resume() 32 } 33 34 override func viewDidLoad() { 35 super.viewDidLoad() 36 // Do any additional setup after loading the view, typically from a nib. 37 } 38 39 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 40 return entries.count 41} 42 43 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 44 let cell = tableView.dequeueReusableCellWithIdentifier("news")! as 45 UITableViewCell 46 47 let entry = entries[indexPath.row] as! NSDictionary 48 49 cell.textLabel?.text = entry["title"] as? String 50 51 return cell 52 } 53} 54
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/02/15 12:46