1
votes

I have a simple game that uses Parse.com for the backend. I originally stored the user values for a Game object as Strings and structured the queries and if statements accordingly (I'm new to programming). I then learned that the right way to do it is to store the users as pointer objects that "point" back to the User table.

I'm struggling to get my if statements to work. I want to check if the current user (PFUser.currentUser()) is equal to one of the users I have stored in the Game table:

//continue an active game that already exists with the user
                    if results != nil{
                        println(results)

                            //continue existing game
                            for result in results {

                                var user1 = result["user1"] as PFUser
                                var user2 = result["user2"] as PFUser
                                var round = result["round"] as Int
                                var turn = result["turn"] as PFUser

                                //continue game if current user is user1
                                if PFUser.currentUser() == user1 {

When I step through my code with breakpoints, the if PFUser.currentUser() == user1 statement is being skipped, even though the statement is true (user 1 is indeed the same user as the currentUser). How do I properly compare the user1 pointer object to the currentUser?

Thanks!

1
Try: PFUser.currentUser().objectId == user1.objectId? - mattdaw
Check out: nshipster.com/equality for some more background. - mattdaw
That worked! Thank you so much! Be sure to add an answer and I'll mark it as the solution - winston
No problem! Wrote up an answer... - mattdaw

1 Answers

1
votes

PFUser isn't set up to support equality in that way, but it looks like comparing objectIds would do the right thing in your case:

if PFUser.currentUser().objectId == user1.objectId