Your First iOS & SwiftUI App: Polishing the App

Mar 1 2022 · Swift 5.5, iOS 15, Xcode 13

Part 2: Swift Coding Challenges

15. Challenge: Start a New Round

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 14. Introduction Next episode: 16. The Xcode Debugger
Transcript: 15. Challenge: Start a New Round

For your first Swift coding challenge, you’re going to add a new method to your Game data model that you can use to start a new round of the game.

We’re going to do this using Test-Driven development. We’ll write the test together, which will fail at first because we haven’t written the code yet. Your challenge will be to pause the video, and the implement the code so that the test passes.

Let’s start with the test.

Add this to BullseyeTests.swift:

func testNewRound() {
  game.startNewRound(points: 100)
  XCTAssertEqual(game.score, 100)
  XCTAssertEqual(game.round, 2)

Show the error.

OK - now it’s your turn. Pause the video, annd write the startNewRound method so that this test passes.

I want to give you a hint: you are going to need to add the “mutating” keyword to this method, because that indicates that when you call this method, it will change some values in the struct itself. So put “mutating func” instead of just func.

Good luck!

Add to Game.swift:

mutating func startNewRound(points: Int) {
  score = score + points
  round = round + 1

Show how you can shorten to:

mutating func startNewRound(points: Int) {
  score += points
  round += 1

Cmd+U to run all tests.