At the whiteboards you found the two constants and seven variables the Guessing Game remembers. Now find out what Swift does when you break its rules. Break it, read the message, then undo.

Break 1: change a constant

  1. Find the function startNewGame(), near the bottom of GuessingGameView.swift.
  2. Add this line just inside its opening brace:
        highestNumber = 50
  1. Wait a moment. A red mark appears beside the line. (If it does not, press Command-B.) Click it to read the whole message:
cannot assign to value: 'highestNumber' is a 'let' constant
  1. Say it in your own words to your neighbour. Why is Swift right to complain?
  2. Press Command-Z until the line is gone.

Break 2: the wrong kind of value

  1. Find this line near the top of the file:
    @State var guessCount: Int = 0
  1. Change 0 to "zero", with the quotation marks.
  2. Read the message:
cannot convert value of type 'String' to specified type 'Int'
  1. What is Swift telling you about "zero" and Int?
  2. Press Command-Z to undo.

Reading a message

Swift’s messages are short, and they use the words you just learned: constant, type, String, Int. Read the message twice before you change anything. The line with the red mark is where Swift noticed the problem. Usually, that is also where the problem is.

If there is time: your first commit of the day

  1. Make one more change from a // TRY: comment: a colour, a symbol, or the wrong-guess message. Leave highestNumber at 100.
  2. Run the game to check that it works.
  3. Save the change with Commit and Push. For the message, write what you changed, such as Changed the colour to teal.

Test of success

  • You have read both messages, and you can say what each one means.
  • Your game runs again, with no red marks.

Curriculum connection

A4.3

demonstrate the ability to interpret error messages displayed by programming tools (e.g., compiler, debugging tool), at different times during the software development process (e.g., writing, compilation, testing);

Link to original