http://qiita.com/stm3/items/7293c54762afeeb10ed5
こちらのサイトを参考にし、URLを読み込むコードを実装し、実行した場合以下のエラーが出ます。
exc_bad_instruction code=exc_i386_invop
output windowにはこちらが
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
swift
1import UIKit 2 3class DetailController: UIViewController { 4 var entry = NSDictionary() 5 6 @IBOutlet weak var webView: UIWebView! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 //read URL by webView 12 let url = NSURL(string: self.entry["link"] as! String) 13 let request = NSURLRequest(URL: url!) 14 webView.loadRequest(request) 15 } 16}
nilチェックができていないのでしょうか?
検索しましたが、解決方法がわかりませんでした。
解決方法をご存知の方はご教授頂けると助かります。
申し訳ございませんが、宜しくおねがいします。
---追記---
ViewController
swift
1import UIKit 2 3class ViewController: UITableViewController { 4 var entries = NSArray() 5 6 let newsUrlString = "https://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 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 54 performSegueWithIdentifier("detail", sender: entries[indexPath.row]) 55 } 56 57 // send entry to DetailController 58 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 59 if segue.identifier == "detail" { 60 // get DetailController 61 let detailController = segue.destinationViewController as! DetailController 62 63 // set entry 64 detailController.entry = sender as! NSDictionary 65 } 66 } 67}
DetailController
swift
1import Foundation 2import UIKit 3 4class DetailController: UIViewController { 5 6 7 @IBOutlet weak var webView: UIWebView! 8 9 var entry = NSDictionary() 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 //read URL by webView 15 let url = NSURL(string: self.entry["link"] as! String)! 16 let request = NSURLRequest(URL: url, cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 60) 17 webView.loadRequest(request) 18 print(url) 19 } 20}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/02/16 14:53
2016/02/16 15:01
2016/02/16 15:53 編集
2016/02/16 16:08 編集
2016/02/17 14:24 編集
2016/02/17 14:35
2016/02/17 14:42
2016/02/18 01:43
2016/02/20 14:56
2016/02/20 15:13 編集
2016/02/20 19:39 編集
2016/02/21 14:18
2016/02/22 01:05
2016/02/22 12:28
2016/02/22 12:34
2016/02/22 12:46
2016/02/22 13:16
2016/02/22 13:25