5-Day Swift Coding Challenge: Day 5

Apr 25 2025 · Swift 6.0, iOS 18, Swift Playground 4.6

Lesson 01: Day 5: Making an App

Demo: Adding SwiftUI Functions

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

When we last left off with this project, we printed out each row on the screen, but we want to process each emoji that composes the array.

func generateRow(rowData: [String], drawPlayer: Bool) -> Text {

}
var textString = ""
for colIndex in 0...rowData.count - 1 {

}
textString += rowData[colIndex]
return Text(textString)
if colIndex == playerCol && drawPlayer {
    textString += "🐰"
} else {
    textString += col[colIndex]
}
generateRow(rowData: data[rowIndex], drawPlayer: playerRow == rowIndex)
  func isValidMove(row: Int, col: Int) -> Bool {
    if data[row][col] == "🟧" {
      return true
    }
    return false
  }
  func isValidMove(row: Int, col: Int) -> Bool {
    return data[row][col] == "🟧"
  }
Button {
    if playerRow > 0 {
        if (isValidMove(row: playerRow - 1, col: playerCol)) {
            playerRow -= 1
        }
    }
} label: {
    Text("⬆️").font(.system(size: 50))
}
Button {
    if playerRow < data.count - 1 {
        if (isValidMove(row: playerRow + 1, col: playerCol)) {
            playerRow += 1
        }
    }
} label: {
    Text("⬇️").font(.system(size: 50))
}
Button {
    if playerCol > 0 {
        if (isValidMove(row: playerRow, col: playerCol - 1)) {
            playerCol -= 1
        }
    }
} label: {
    Text("⬅️").font(.system(size: 50))
}
Button {
    if playerCol < data[0].count - 1 {
        if (isValidMove(row: playerRow, col: playerCol + 1)) {
            playerCol += 1
        }
    }
} label: {
    Text("➡️").font(.system(size: 50))
}
@State var wonGame = false
var carrotRow = 10
var carrotCol = 3
if (data[row][col] == "🟧" || data[row][col] == "🥕") {
wonGame = playerRow == carrotRow && playerCol == carrotCol
.alert("Congrats! You won!", isPresented: $wonGame) {
    Button("OK") { }
}
See forum comments
Cinema mode Download course materials from Github
Previous: Understanding Functions Next: Conclusion