我正在尝试从Facebook登录添加到Parse用户。
登录有效,但是当我想从Facebook用户那里获取数据时,我的应用程序崩溃说:"致命错误:在展开可选值(lldb)"时意外发现nil; 它来自" firstName"的价值。这是零(见下面的代码) 知道为什么吗?会很棒:)
var fbRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
fbRequest.startWithCompletionHandler({ (FBSDKGraphRequestConnection, result, error) -> Void in
if (error == nil && result != nil) {
let facebookData = result as! NSDictionary //FACEBOOK DATA IN DICTIONARY
let facebookID = (facebookData.objectForKey("id") as? String)
let userEmail = (facebookData.objectForKey("email") as? String)
let firstName = (facebookData.objectForKey("first_name") as? String)
let lastName = (facebookData.objectForKey("last_name") as? String)
var user = PFUser()
// println(firstName!) -> says that firstName is nil: " fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)"
user.setValue(firstName, forKey: "firstName")
user.setValue(lastName, forKey: "username")
user.setValue(facebookID, forKey: "objectID")
user.setValue(userEmail, forKey: "email")
user.signUpInBackgroundWithBlock { .... }
我完全不知道如何解决这个问题,所以任何帮助都会很棒! :)
PS:此代码在
中 func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) { ... }
答案 0 :(得分:2)
尝试使用键字段
提供参数 if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, email, first_name, last_name"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
let email = result.valueForKey("email") as? String
let fbid = result.valueForKey("id") as! String
let fname = result.valueForKey("first_name") as! String
let lname = result.valueForKey("last_name") as! String
}
})
}