Reputation: 8608
This should be really straightforward but I have no idea why this code is not working. I try to load a basic URL into a UIWebView
like this:
let string = "www.google.com".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
let url = NSURL(string: string!)
if let ur = url {
let request = NSURLRequest(URL: ur)
webView.loadRequest(request)
}
However each time I get this error:
fatal error: unexpectedly found nil while unwrapping an Optional value
Stepping through it nothing is nil
. Figured I'd do this just to make sure:
let string = "www.google.com".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
// unwrap the string
if let str = string {
let url = NSURL(string: str)
// unwrap the url
if let ur = url {
let request = NSURLRequest(URL: ur)
webView.loadRequest(request)
}
}
Which I still get the same crash! If I look at the thread I get this when running on a device:
0x656d6c <+68>: bl 0x6abac4 ; function signature specialization 1 = Exploded> of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)
So my UIWebView
is nil
somehow...I have everything hooked up correctly? Any ideas
Upvotes: 0
Views: 2393
Reputation: 8608
@penatheboss answers was correct however I wanted to post how I fixed it. My IBOutlets
were hooked up correctly however my UIWebView
was always nil
in my class. Even in viewDidAppear
.
How I fixed it:
Then it worked fine with the original code I posted. Talk. About. Annoying.
Upvotes: 0
Reputation: 7736
The WebView itself must be nil
. If there is an empty circle to the left of the outlet, reconnect it by dragging and dropping from the view to the outlet.
Upvotes: 1