Etgar Etgar - 1 year ago 335
iOS Question

Swift 3 - URLRequest creation crashes

let url = URL(string: "\(SERVER_HOST)/post?fb_id=\(fb_user)")
var request = URLRequest(url: url!) // Crashes here

.responseJSON { response in
switch response.result {
case .failure(let error):
onComplete(success: false)
case .success(let responseObject):
onComplete(success: true)

The crash error:

fatal error: unexpectedly found nil while unwrapping an Optional value

It worked before the Swift 3.

It was NSURLRequest and it worked. What can I do in order to fix it?

Answer Source

Well, the solution was to add ! after each variable in the string formatting in order to make it non-optional.

let stringUrl = "\(SERVER_HOST)...\(!)..."
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download