Вот мой соответствующий код:
Код: Выделить всё
override func viewDidLoad() {
super.viewDidLoad()
fbLoginButton.delegate = self
fbLoginButton.readPermissions = ["email", "public_profile", "user_location"]
}
extension RegisterViewController: FBSDKLoginButtonDelegate {
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
print("Did log out of facebook")
}
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
if error != nil {
print(error)
return
}
print("Successfully logged in with facebook...")
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, first_name, last_name, email, location{location}"]).start { (connection, result, err) in
if err != nil {
print("Failed to start graph request:", err)
return
}
print(result)
let fbDetails = result as! NSDictionary
let location : NSDictionary! = fbDetails.value(forKey: "location") as! NSDictionary //code crashes here
let locationContents : NSDictionary! = location.value(forKey: "location") as! NSDictionary
let city: String! = locationContents.value(forKey: "city") as? String
let state: String! = locationContents.value(forKey: "state") as? String
let country: String! = locationContents.value(forKey: "country") as? String
print(fbDetails)
print("city:", city)
print("state:", state)
print("country:", country)
}
}
}
Код: Выделить всё
fatal error: unexpectedly found nil while unwrapping an Optional value
Код: Выделить всё
let location : NSDictionary! = fbDetails.value(forKey: "location") as! NSDictionary
Подробнее здесь: https://stackoverflow.com/questions/449 ... k-in-swift
Мобильная версия