Use of local variable 'flipCard' before its declaration

Hello,

we tried to build the app from the lecture 1 of "Developing IOS 11 Apps with swift" with Paul Hegarty and we got this message. Can anybody help, please?

Thanks a lot.

Here is the code:

/

/

/

/

/

import UIKit

class ViewController: UIViewController

{

var flipCount = 0 {

didSet {

flipCountLabel.text = "Flips:\(flipCount)"

}

}

@IBOutlet weak var flipCountLabel: UILabel!


@IBOutlet var cardButtons: [UIButton]!


var emojiChoices = [" "," "," "," "]


@IBAction func touchCard(_ sender: UIButton) {

flipCount += 1

if let cardNumber = cardButtons.index(of: sender) {

flipCard(withEmoji: emojiChoices[cardNumber], on: sender) //Message: Use of local variable 'flipCard' before its declaration

} else {

print ("chosen card was not in cardButton")

}




func flipCard(withEmoji emoji: String, on button: UIButton){

if button.currentTitle == emoji {

button.setTitle("", for: UIControlState.normal)

button.backgroundColor = colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)

} else {

button.setTitle(emoji, for: UIControlState.normal)

button.backgroundColor = colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)

}

}

}

}

I'm not a Swift guy but it appears you have that flipCard function declared inside the scope of the touchCard function. I would think it should be outside that function but inside the class. So my suggestion would be to move one of those closing curly braces to before the flipCard function.

You could also move the code for the func before its use in the IBAction, if you want to keep it inside.


    @IBAction func touchCard(_ sender: UIButton) {
       
        func flipCard(withEmoji emoji: String, on button: UIButton){
            if button.currentTitle == emoji {
                button.setTitle("", for: UIControlState.normal)
                button.backgroundColor =  colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
            } else {
                button.setTitle(emoji, for: UIControlState.normal)
                button.backgroundColor =  colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
            }
        }
       
        flipCount += 1
        if let cardNumber = cardButtons.index(of: sender) {
            flipCard(withEmoji: emojiChoices[cardNumber], on: sender)     //Message: Use of local variable 'flipCard' before its declaration
        } else {
            print ("chosen card was not in cardButton")
        }
       
    }
Use of local variable 'flipCard' before its declaration
 
 
Q