user3255746 user3255746 - 1 year ago 113
Swift Question

Facebook crash with empty email in graph

I am a bit new to swift and could use some help. I am successfully running a Facebook SDK graph request, and saving pieces of it to my database on Parse. The issue is that if the user does not allow their email to be read, my app crashes because the object is empty. Here is my code:

FBSDKGraphRequest(graphPath: "me",
parameters: ["fields": "id, name,friends, first_name, last_name, picture.type(large), email, gender,taggable_friends"])
.start(completionHandler: { (connection, result , error) -> Void in

let data:[String:AnyObject] = result as! [String : AnyObject]
PFUser.current()!["fb_id"] = data["id"] as? String
PFUser.current()!["name"] = data["name"] as? String

PFUser.current()!["email"] = data["email"] as! String
PFUser.current()!["gender"] = data["gender"] as! String

Can someone help me edit the email line to say if it is empty do not attempt to force save it?

Answer Source

One way is the standard way to safely unwrap an optional in Swift:

if let email = data["email"] as? String {
    PFUser.current()!["email"] = email
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download