简体   繁体   中英

How to compare pointer parse objects in Swift?

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!

PFUser并非以这种方式设置为支持相等性,但是在您的情况下,似乎比较objectIds会做正确的事情:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM