我正在尝试在我的应用程序中显示 WebView 。 webview 的 url 是对象格式,因为我是从 json 获取的。因此,在我的代码中,我将对象转换为 NSString 并尝试加载 View ,但我的应用程序崩溃并提示: fatal error :在展开可选值时意外发现 nil。有没有更好的方法将对象转换为字符串?如何让 webview 显示 url 内容?
制作详细信息的地方:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let object: AnyObject = objectsPath[indexPath.row]
(segue.destinationViewController as DetailViewController).detailItem = object
}
}
}
这是我的代码:
var detailItem: AnyObject? {
didSet {
// Update the view.
self.configureView()
}
}
func configureView() -> NSURLRequest {
// Update the user interface for the detail item.
let detail: AnyObject = self.detailItem!
let stringURL: String = String(detail as NSString)
let url = NSURL(string: stringURL)
let request = NSURLRequest(URL: url!) <---ERROR occurs here
return request
// let string:St= detailItem!
// previewWebView.
// println(string)
//let url = NSURL(string: string)
//let request = NSURLRequest(URL: url!)
//self.previewWebView.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.previewWebView.loadRequest(configureView())
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最佳答案
您在 viewDidLoad()
中调用 configureView()
,而不考虑 self 的值。详细信息项目
。您应该在 viewDidLoad()
中调用此函数之前检查一个值,或者您应该修改 configureView()
以不声明肯定有一个值 self。详细信息
。
关于ios - 应用程序不断崩溃发现 Nil Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26939085/