Search code examples
iosswiftxcodememberxcode9

Type has no member - App Development Project 2, Apple Pie


I'm working through App Development with Swift, and I'm stuck on the last bit of the second project, Apple Pie.

In the guide, it has you create a new file, Game.Swift, and a struct Game inside that file. Later on, it tells you to

Create a computed property called formattedWord within the definition of Game. Here's one way to compute formattedWord:

var formattedWord: String {
    var guessedWord = ""
    for letter in word.characters {
        if guessedLetters.contains(letter) {
            guessedWord += "\(letter)"
        } else {
            guessedWord += "_"
        } 
    }
    return guessedWord
}

 

Now that formattedWord is a property that your UI can display, try using it for the text of currentWordLabel inside of updateUI().

func updateUI() {
    correctWordLabel.text = game.formattedWord
    scoreLabel.text = "Wins: \(totalWins), Losses: \(totalLosses)"
    treeImageView.image = UIImage(named: "Tree \(currentGame.incorrectMovesRemaining)")
}

I've done this EXACTLY, I've checked the code and the guide about 700 times, not to mention google searches and looking at a similar (but vaguely answered) question here on SO, but I still can't get it fixed. I found a solution that said to change

correctWordLabel.text = game.formattedWord

to

correctWordLabel.text = currentGame.formattedWord

but that's not working, either. If anyone can help, I would surely appreciate it. I've attached scrseenshots, just for further clarification.

View Controller screenshot

struct Game screenshot

I feel like I somehow haven't declared formattedWord correctly inside the struct Game, but I've done everything the guide says exactly. I'm still getting

Type 'Game' has no member 'formattedWord'

on line 45 of the View Controller. Please help!


Solution

  • If you select all the code, and auto indent it (keyboard short cuts: ctrl+a, then ctrl+i). You would see the problem pretty quickly as the resulting code would look like

    struct Game {
        var word: String
        var incorrectMovesRemaining: Int
        var guessedLetters: [Character]
        mutating func playerGuessed(letter: Character) {
            guessedLetters.append(letter)
            if !word.characters.contains(letter) {
                incorrectMovesRemaining -= 1
    
                var formattedWord: String {
                    var guessedWord = ""
                    for letter in word.characters {
                        if guessedLetters.contains(letter) {
                            guessedWord += "\(letter)"
                        } else {
                            guessedWord += "_"
                        }
                    }
                    return guessedWord
                }
            } // <-- This is where your problem is
        }
    }
    

    In the code you currently have, you're declaring the formattedWord variable inside the playersGuessed function, so nothing outside of that function can access that variable.

    Your code isn't properly formatted in your Game struct. Notice the 3 } on lines 31->33. That is a big red flag. The problem is that you didn't close the if statement on line 17, and the function on line 15. You need to add the closing } to line 19/20. Your Game struct should look like this.

    struct Game {
        var word: String
        var incorrectMovesRemaining: Int
        var guessedLetters: [Character]
        mutating func playerGuessed(letter: Character) {
            guessedLetters.append(letter)
            if !word.characters.contains(letter) {
                incorrectMovesRemaining -= 1
            } // <-- This is where your problem is
        }
    
        var formattedWord: String {
            var guessedWord = ""
            for letter in word.characters {
                if guessedLetters.contains(letter) {
                    guessedWord += "\(letter)"
                } else {
                    guessedWord += "_"
                }
            }
            return guessedWord
        }
    }