AppDelegate:在展开可选值时意外发现 nil
AppDelegate: unexpectedly found nil while unwrapping an Optional value
我正在从 AppDelegate 进行简单调用:
let vc = ViewController()
vc.myaction()
该操作在 Web 视图对象(在 didload 覆盖上加载)中打开一个 url
这是函数:
func myaction() {
let url = URL(string: "http://192.168.1.5/doit")
let urlreq = URLRequest(url: url!)
mywebview.loadRequest(urlreq)
}
这是错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
found nil 可能是 mywebView
因为它只会在您的 viewcontroller
视图加载时初始化。尝试使用以下代码
func myaction() {
let mywebview = UIWebView()
let url = URL(string: "http://192.168.1.5/doit")
let urlreq = URLRequest(url: url!)
mywebview.loadRequest(urlreq)
}
或者您可以在 myaction()
之外初始化 mywebview。
试试 NSURL:
func myaction() {
let url = NSURL(string: "http://192.168.1.5/doit")
let urlreq = NSURLRequest(url: url!)
mywebview.loadRequest(urlreq)
}
我正在从 AppDelegate 进行简单调用:
let vc = ViewController()
vc.myaction()
该操作在 Web 视图对象(在 didload 覆盖上加载)中打开一个 url
这是函数:
func myaction() {
let url = URL(string: "http://192.168.1.5/doit")
let urlreq = URLRequest(url: url!)
mywebview.loadRequest(urlreq)
}
这是错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
found nil 可能是 mywebView
因为它只会在您的 viewcontroller
视图加载时初始化。尝试使用以下代码
func myaction() {
let mywebview = UIWebView()
let url = URL(string: "http://192.168.1.5/doit")
let urlreq = URLRequest(url: url!)
mywebview.loadRequest(urlreq)
}
或者您可以在 myaction()
之外初始化 mywebview。
试试 NSURL:
func myaction() {
let url = NSURL(string: "http://192.168.1.5/doit")
let urlreq = NSURLRequest(url: url!)
mywebview.loadRequest(urlreq)
}