1

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!

4
  • 1
    Try: PFUser.currentUser().objectId == user1.objectId? Commented Apr 24, 2015 at 12:58
  • Check out: nshipster.com/equality for some more background. Commented Apr 24, 2015 at 12:59
  • That worked! Thank you so much! Be sure to add an answer and I'll mark it as the solution Commented Apr 24, 2015 at 13:46
  • No problem! Wrote up an answer... Commented Apr 24, 2015 at 13:49

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.