winston
winston

Reputation: 3100

Can't use nil for keys or values on PFObject. Use NSNull for values

I get the following error when I try to save Parse objectId as a Pointer value for the User class: Can't use nil for keys or values on PFObject. Use NSNull for values.

I have a UICollectionView that shows a list of avatars (Avatar class in Parse). When a user selects an avatar in the list, I want to set that User's avatar to the selected image using a Pointer value.

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print(self.avatars[indexPath.row])

        self.user["avatar"] = self.avatars[indexPath.row]["objectId"]

        self.performSegueWithIdentifier("returnToProfile", sender: user)
    }

}

User has Pointer column to the Avatar class. Trying to get the Avatar objectId to place into the column in the User table but I keep getting the error. What's odd is that print(self.avatars[indexPath.row]) returns the following:

<Avatar: 0x7f8ebb835c80, objectId: WG7L0KpdJ2, localId: (null)> {
    file = "<PFFile: 0x7f8ebb837dc0>";
    name = man;
}

objectId exists, but if I try to print with print(self.avatars[indexPath.row]["objectId") I get nil.

What am I doing wrong?

Thanks!

EDIT: Updated code:

let user = PFUser.currentUser()

user!["avatar"] = self.avatars[indexPath.row].objectId

do {
   print(self.avatars[indexPath.row].objectId)
   try user?.save()
} catch {
    print(error)
}

the print statement prints the correct objectId. However, saving it to the user causes error invalid type for key avatar, expected *Avatar, but got string" UserInfo={code=111, temporary=0, error

EDIT 2: Pointers don't save the objectId. You have to save the ENTIRE OBJECT. Just figured this out. Here's the updated code that now works. Thanks for the help everyone!!

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        self.user["avatar"] = self.avatars[indexPath.row]

        do {
           try self.user.save()
        } catch {
            print(error)
        }

        self.performSegueWithIdentifier("returnToProfile", sender: user)
    }

}

Upvotes: 0

Views: 732

Answers (1)

pbush25
pbush25

Reputation: 5258

To access the ID: self.user["avatar"] = self.avatars[indexPath.row].objectId

In this case, objectId is a property of the object, not one of it's dictionary subscripted values.

Upvotes: 1

Related Questions