3. @IBAction should call other methods instead executing a logic code. Example: One would trigger a game start method and an analytics event once a button tapped.
private var x: Int?
private var y: Int?
@IBAction func startButtonPressed(_ sender: UIButton) {
gameStartButtonPressed()
Analytics.logAction("startButtonPressed")
}
private func gameStartButtonPressed() {
guard let numberOfRooms = selectedRooms.text, !numberOfRooms.isEmpty else {
return selectedRooms.placeholder = "type it, dude"
} //if user typed something
let rooms = Int(numberOfRooms)
x = Int(ceil(sqrt(Double(rooms!))))
y = x //grab some values from user input
guard let x = x, y = y else {
return
}
guard let maze = MazeGenerator(x, y) else { //generate a maze
return
}
hp = 2 * (x * y) //get hp value depending on user input
hpLabel.text = "hp: \(hp)"
guard let currentX = getRandomX(x), let currentY = getRandomY(y) else {
return
}
currentCell = maze.maze[currentX][currentY] //game starts in a random part of the maze
for item in items.indices { //create and show some imageViews
items[item].location = (getRandomX(x!), getRandomX(y!))
createItems(imageName: items[item].imageName)
if items[item].location == (currentX!, currentY!) {
reveal(index: item)
} else {
hide(index: item)
}
}
refreshButtons() //refresh UI
maze.display() //print maze scheme in debug console
}
4. Separate a model from a controller
struct Model {
var x: Int
var y: Int
}
private var position: Model = .init(x: 0, y: 0)
// instead of:
private var x: Int?
private var y: Int?
Additional insights: