'If if statement returns true never execute that function
I've a function with if statement inside it, I want to check everytime person clicks, whenever the if statement returns true, I want that function to be removed or never again called, is it possible to achieve in swift?
func checkLevelUp(){
if CookieViewController.moneyLevel >= 3 && CookieViewController.bonusLevel >= 3 && CookieViewController.spinLevel >= 3 {
print("Level up !!!!!!!") // if this is printed, I never want checkLevelUp function to exists
}
}
Solution 1:[1]
You need to store this particular state outside the scope of this function.
var didLevelUp = false
func checkLevelUp() {
guard !didLevelUp else {
return // return out of the function if `didLevelUp` is not false
}
if CookieViewController.moneyLevel >= 3 &&
CookieViewController.bonusLevel >= 3 &&
CookieViewController.spinLevel >= 3 {
print("Level up !!!!!!!")
didLevelUp = true
}
}
How you store it outside the scope of the function is up to you but this is the solution you're looking for.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fake girlfriends |
