gpt4 book ai didi

ios - 根据不同的条件返回不同的值

转载 作者:行者123 更新时间:2023-11-30 11:44:23 26 4
gpt4 key购买 nike

我得到了以下代码,但它给出了错误“预期返回‘Int’的函数中缺少返回”那么编写此代码以根据不同的 if 条件给出不同的返回值的正确方法是什么?

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if category == "followings"{
return usersRealNameFollowing.count
}
if category == "followers"{
return usersRealNameFollower.count
}
}

谢谢!

最佳答案

当函数有返回类型时,应该有一个必须返回的语句,意味着函数应该总是返回一些东西。现在,在您的情况下,您对两个返回语句都使用 if 语句,这意味着如果两个语句都错误,则将没有返回值。在这里使用 if-else 这将是最好的选择或

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count: Int()
if category == "followings"{
count = usersRealNameFollowing.count
}else if category == "followers"{
count = usersRealNameFollower.count
}
return count
}

关于ios - 根据不同的条件返回不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49000794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com