swift - Return to beginning of a function in Swift
问题描述
So I've following code running in Xcode 12 with SwiftUI:
public func giveCard() -> String {
var randomNumber = Int.random(in: 0..<maxCards-1)
if saveRandoms.contains(randomNumber) {
print("Already exist - generate new number")
} else {
let actualCard = questionCard[randomNumber]
saveRandoms.add(randomNumber)
return actualCard
print("Schicke frage")
}
return "No question-card left"
}
The code creates random numbers between 0 and the variable maxCards. When a questionCard fitting to the random number which was created was used, the random number is stored in the array saveRandoms.
To avoid showing the same question the if-statement checks if the new created random number is already in the array saveRandoms. If not, the fitting question will be shown.
If the number was already used it should generate a new number until finding a unused one. And that's my problem. How can I return to the beginning of my function to check again and again if the random number was already used?
解决方案
我认为最好的解决方案是将逻辑置于while
循环中,并在找到新数字时完成执行。
推荐阅读
- apache-spark - 生成不相交的 Spark DataFrame 集
- asp.net - VS2019 AWS 工具包安装失败
- amazon-web-services - terraform :尚未声明名为“AMI”的输入变量。这个变量可以用变量“AMI”{}块声明
- python - 从一列到下一列的百分比变化
- python - 使用词词典进行垃圾邮件分类
- python - Python:遍历文件夹并从每个文件的第一个选项卡保存数据并保存到单独选项卡上的新文件
- pointers - 保存以构造本地指针
- html - CSS - 确保我的 CSS 网格的前两行等于屏幕的剩余高度(不包括标题)
- c++ - 如何从 std::thread 返回值
- php - 如果一个字段为空,则将第二个分配给变量