title: unwrapping an Optional value
body: <p>I am trying to login with macbook using code but I keep on getting this error:</p>
  
  <blockquote>
    <p>Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value</p>
  </blockquote>
  
  <pre><code>// API to log an user in
  func login(userType: String, completionHandler: @escaping (NSError?) -&gt; Void) {
  
      let path = "api/social/convert-token/"
      let url = baseURL!.appendingPathComponent(path)
      let params: [String: Any] = [
          "grant_type": "convert_token",
          "client_id": CLIENT_ID,
          "client_secret": CLIENT_SECRET,
          "backend": "facebook",
          "token": FBSDKAccessToken.current().tokenString,
          "user_type": userType
      ]
  
      Alamofire.request(url!, method: .post, parameters: params, encoding: URLEncoding(), headers: nil).responseJSON { (response) in
  
          switch response.result {
          case .success(let value):
  
              let jsonData = JSON(value)
  
              self.accessToken = jsonData["access_token"].string!
              self.refreshToken = jsonData["refresh_token"].string!
              self.expired = Date().addingTimeInterval(TimeInterval(jsonData["expire_in"].int!))
  
              completionHandler(nil)
              break
  
          case .failure(let error):
              completionHandler(error as NSError?)
              break
          }
      }
  
  }
  </code></pre>
  
  <p>The error is referring to this line:</p>
  
  <pre><code>self.accessToken = jsonData["access_token"].string!
  </code></pre>
  
  <p>and this is LoginViewController code:</p>
  
  <pre><code>import UIKit
  import FBSDKLoginKit
  
  class LoginViewController: UIViewController {
      @IBOutlet weak var bLogin: UIButton!
      @IBOutlet weak var bLogout: UIButton!
  
      var fbLoginSuccess = false
      var userType: String = USERTYPE_CUSTOMER
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          if (FBSDKAccessToken.current() != nil) {
              bLogout.isHidden = false
              FBManager.getFBUserData(completionHandler: {
  
                  self.bLogin.setTitle("Continue as \(User.currentUser.email!)", for: .normal)
                  // self.bLogin.sizeToFit()
              })
          }
      }
  
      override func viewDidAppear(_ animated: Bool) {
          if (FBSDKAccessToken.current() != nil &amp;&amp; fbLoginSuccess == true) {
              performSegue(withIdentifier: "CustomerView", sender: self)
          }
      }
  
      @IBAction func facebookLogout(_ sender: AnyObject) {
          APIManager.shared.logout { (error) in
              if error == nil {
                  FBManager.shared.logOut()
                  User.currentUser.resetInfo()
  
                  self.bLogout.isHidden = true
                  self.bLogin.setTitle("Login with Facebook", for: .normal)
              }
          }
      }
  
      @IBAction func facebookLogin(_ sender: AnyObject) {
          if (FBSDKAccessToken.current() != nil) {
              APIManager.shared.login(userType: userType, completionHandler:  { (error) in
                  if error == nil {
                      self.fbLoginSuccess = true
                      self.viewDidAppear(true)
                  }
              })
          } else {
              FBManager.shared.logIn(
                  withReadPermissions: ["public_profile", "email"],
                  from: self,
                  handler: { (result, error) in
                      if (error == nil) {
                          FBManager.getFBUserData(completionHandler: {
                              APIManager.shared.login(userType: self.userType, completionHandler:  { (error) in
                                  if error == nil {
                                      self.fbLoginSuccess = true
                                      self.viewDidAppear(true)
                                  }
                              })
                          })
                      }
              })
          }
      }
  }
  </code></pre>
  
  <p>Swift 3
  Xcode 9
  iOS 10.2</p>
  
  <p>I read several texts to find out the causes of this type of error but not succeed.</p>