1

I have some trouble passing data between parse and user ID facebook , I have this error below :

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use nil for keys or values on PFObject. Use NSNull for values.

Can you help me please !

My code between FacebookSDK and parse to get the information from users

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
        self.Username.delegate = self
        self.Name.delegate = self
        self.emailUser.delegate = self
        self.genderUser.delegate = self
        
        //import data from facebook
        
        
        
        let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, gender, email"])
        graphRequest.startWithCompletionHandler( {
            
            
            (connection, result, error) -> Void in
            
            if error != nil {
                
                print( error)
                
                
            } else if let result = result {
                
                PFUser.currentUser()?["gender"] = result["gender"]
                PFUser.currentUser()?["name"] = result["name"]
                PFUser.currentUser()?["email"] = result["email"]
                
                
                PFUser.currentUser()?.save()
                
                
                
                let userId = result ["id"] as! String
                
                
                
                let facebookProfilePictureUrl = "https://graph.facebook.com/" + userId + "/picture?type=small"
                
                if let fbpicUrl = NSURL(string: facebookProfilePictureUrl) {
                    
                    if let data = NSData(contentsOfURL: fbpicUrl) {
                        
                        self.imageUser.image = UIImage(data: data)
                        
                        let imageFile:PFFile = PFFile(data: data)
                        
                        
                        
                        PFUser.currentUser()?["image"] = imageFile
                        PFUser.currentUser()?.save()
                        
                        //Insert info user in profile
                        
                        
                        self.Name.text = PFUser.currentUser()?["name"] as? String
                        self.genderUser.text = PFUser.currentUser()?["gender"] as? String
                        self.emailUser.text = PFUser.currentUser()?["email"] as? String
                        
                        
                    }
                    
                }
                
            }
            
        })

1 Answer 1

1

Just like the error says, either one of these lines are assigning nil to the PFUser object:

PFUser.currentUser()?["gender"] = result["gender"]
PFUser.currentUser()?["name"] = result["name"]
PFUser.currentUser()?["email"] = result["email"]
PFUser.currentUser()?["image"] = imageFile

Find out which of the values in result are nil and take action accordingly. Since Parse doesn't support the object pointer 'nil', you will have to use [NSNull null].

EDIT

I'm pretty new to Swift myself but here is a possible solution to your problem. You could for instance do a check like this:

PFUser.currentUser()?["gender"] = result["gender"] ?? NSNull()

This includes a ternary operation to the assignment, which basically says 'if result["gender"] is not nil, then return it, else return NSNull()'. This solution is of course only valid if you don't mind that Parse objects have NSNull() values.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer ! I'm new in swift, what do I have to change in my code ? thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.