Why doesn't return return stop the program?

So, I have a function that checks if the user is viewing their profile or not. and here is my function.

func isUserViewCurrentUser() -> Bool {
    guard let user = userDataDelegate?.userData() else {
        return false
    }

    if user == Current.user {
        print("returning false: USER IS CURRENT USER")
        return false
    } else {
        print("returning true: USER IS NOT CURRENT USER")
        return true
    }

 }

      

Then I will only run my function in the view, the code should stop after I type something:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    if isUserViewCurrentUser() == true {
        self.navigationItem.rightBarButtonItem = nil
        self.navigationItem.leftBarButtonItem = nil
        return
    } else {
        return
    }

    if let userID = userDataDelegate?.userData().userID {
        retrieveFollowerNumbers(userIdentifier: userID)
        return
    }

 }

      

But my console was typing "returning true: USER IS NOT USER ONLY" for a thousand times and never stops. I've looked everywhere for code that I never run isUserViewCurrentUser()

in a for loop or any loop.

This is my userData:

protocol SendUserDataDelegate {

    func userData() -> User
}

      

didSelect TableViewCell:

if let user = filteredUsers?[indexPath.row] {
        userToPass = user
        profileController?.userDataDelegate = self
        self.navigationController?.pushViewController(profileController!, animated: true)

    }

      

And this code on my cellRowAtIndexPath:

if isUserViewCurrentUser() == true {
            cell.bioAndFollowStackView.addSubview(cell.followButtonOutlet)
        } else {
            if cell.followButtonOutlet != nil {
                cell.followButtonOutlet.removeFromSuperview()
            }
        }

      

dispatching a custom object when the user inserts a table cell into another viewController. and I call it whenever I want to get a reference to the user object

+3


source to share


2 answers


Personally, I see that you have 2 objects with different references and you are checking if they are equal ... even if the data inside is the same, the actual references are not.

If so, you can check them with something unique like id.

if user.id == Current.user.id {}



I don't know what you are trying to achieve, but in my personal opinion (no offense) there is something smelly ... :). You can think of a better solution.

Hope this helps, wish you all the best.

+1


source


In viewWillAppear

you call isUserViewCurrentUser()

, which is calling userDataDelegate.userData()

. You haven't said what it does userData()

, but I bet it makes your view reappear, or triggers again isUserViewCurrentUser()

.



Anyway post userData()

code ...

0


source







All Articles