Reputation:
I have four functions which I want to activate randomly. This is the code I have so far, which I get no errors from. However, I get an error "Expression Resolves to an unused function" when I try to call the createSquare() function.
func createSquare () -> () -> () {
let squares = [self.squareOne, self.squareTwo, self.squareThree, self.squareFour]
let randomSquare = Int(arc4random_uniform(UInt32(squares.count)))
return squares[randomSquare]
}
if gameIsOn == true {
createSquare()
}
How can I successfully call the function createSquare()?
func squareOne() {
square1.isHidden = false
}
And squareTwo, squareThree, squareFour are all similar with different numbers.
Upvotes: 1
Views: 120
Reputation: 7
func createSquare() {
let randomNumber = Int.random(in: 0...3)
if randomNumber == 0 {
squareOne()
} else if randomNumber == 1 {
squareTwo()
} else if randomNumber == 2 {
squareThree()
} else if randomNumber == 3 {
squareFour()
}
}
Upvotes: 0
Reputation: 726849
The problem is that createSquare()
returns a closure that you never invoke (that's the "unused function" mentioned in the warning message).
You need to call the function, for example, like this:
let randomFunction = createSquare()
randomFunction()
You can also call it right away with a somewhat less readable syntax:
createSquare()()
Upvotes: 2