为什么返回语句不能停止程序?

时间:2021-02-04 19:58:01

So I have a function that checks wether 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 only run my function in the view will appear, the code supposed to stop after I print 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 printed "returning true: USER IS NOT CURRENT USER" for a thousand times and never stop. I've looked everywhere in my code that I never run isUserViewCurrentUser() in a for loop or any loop.

但是我的控制台打印了“返回真:用户不是当前用户”一千次,而且永远不会停止。我在代码中查找了所有我从未在for循环或任何循环中运行isUserViewCurrentUser()的地方。

This is my userData:

这是我的用户数据:

protocol SendUserDataDelegate {

    func userData() -> User
}

didSelect TableViewCell:

didSelect TableViewCell:

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

    }

And this code at my cellRowAtIndexPath:

我的cellRowAtIndexPath中的代码:

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

sending user object when user tabs a tableview cell into another viewController. and I call it whenever I want to get a reference to the user object

当用户将一个tableview单元格切换到另一个viewController中时,发送用户对象。每当我想要获取对user对象的引用时,我就调用它

2 个解决方案

#1


1  

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

我个人认为你有两个不同引用的对象,你检查它们是否相等。即使内部的数据是相同的,实际的引用也不是。

If that is the case, you can check them by something unique like id.

如果是这种情况,您可以使用id之类的惟一方法检查它们。

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

如果用户。id = = Current.user。{ id }

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

我不知道你想要达到什么目标,但在我个人看来(无意冒犯),有些东西很难闻……:)。你可以想出更好的解决办法。

Hope this will help, wish you all the best.

希望这对你有所帮助,祝你一切顺利。

#2


0  

In viewWillAppear you call isUserViewCurrentUser(), which calls userDataDelegate.userData(). You didn't post what userData() does, but I'll bet it either causes your view to appear again, or calls isUserViewCurrentUser() again.

在viewWillAppear中,您调用isUserViewCurrentUser(),它调用userdatadelegat . userdata()。您没有发布userData()所做的事情,但我敢打赌它会导致您的视图再次出现,或者再次调用isUserViewCurrentUser()。

In any case, please post userData() code...

无论如何,请发布userData()代码…

#1


1  

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

我个人认为你有两个不同引用的对象,你检查它们是否相等。即使内部的数据是相同的,实际的引用也不是。

If that is the case, you can check them by something unique like id.

如果是这种情况,您可以使用id之类的惟一方法检查它们。

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

如果用户。id = = Current.user。{ id }

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

我不知道你想要达到什么目标,但在我个人看来(无意冒犯),有些东西很难闻……:)。你可以想出更好的解决办法。

Hope this will help, wish you all the best.

希望这对你有所帮助,祝你一切顺利。

#2


0  

In viewWillAppear you call isUserViewCurrentUser(), which calls userDataDelegate.userData(). You didn't post what userData() does, but I'll bet it either causes your view to appear again, or calls isUserViewCurrentUser() again.

在viewWillAppear中,您调用isUserViewCurrentUser(),它调用userdatadelegat . userdata()。您没有发布userData()所做的事情,但我敢打赌它会导致您的视图再次出现,或者再次调用isUserViewCurrentUser()。

In any case, please post userData() code...

无论如何,请发布userData()代码…