Search code examples
iosnsurlrequesturlrequestswift3xcode8

Swift 3 - URLRequest creation crashes


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

        Alamofire.request(request)
            .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?


Solution

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

    let stringUrl = "\(SERVER_HOST)...\(FBUser.id!)..."