App crashes when buttons pressed

First i must say i am a total newbie. I "learned" the bit of swift i have used in the last 12-24 hours. Let alone using xcode..


I am having an issue with the application i am creating (for iOS). The app is basically a life counter with + and - buttons. When i press the Player 2 + button it works. No issues. But when i press the Player 2 - button it crashes. Respectively the Player 1 buttons both crash the app outright. The buttons are simple buttons with the + and - in them. the life variable/value is a simple label with a 20 in it. Does anyone see anything in this that would crash the appl


var p1count = 20
    var p2count = 20
    var p3count = 20
    @IBOutlet weak var Player1Life: UILabel!
    @IBAction func P1Add(_ sender: UIButton){P1increaseCount()}
    @IBAction func P1Minus (_ sender: UIButton){P2decreaseCount()}
    
    
    func P1increaseCount () {
        p1count += 1
        Player1Life.text = String(p1count)
    }
    
    func P1decreaseCount () {
        p1count -= 1
        Player1Life.text = String(p1count)
    }
    
    @IBOutlet weak var Player2Life: UILabel!
    
    @IBAction func P2Add(_ sender: UIButton) {P2increaseCount()}
    
    @IBAction func P2Minus(_ sender: UIButton){P2decreaseCount()}

    
    func P2increaseCount () {
        p2count += 1
        Player2Life.text = String(p2count)
    }
    
    func P2decreaseCount () {
        p2count -= 1
        Player2Life.text = String(p2count)
    }
}

Accepted Reply

Have you checked you connected all IBOutlets properly as well as IBAction ?


Could try to clean build folder (option clean in Product menu).


Some comments on this code:


func names should begin with lowercase : func p2increaseCount, p2Add, …

Replies

Have you checked you connected all IBOutlets properly as well as IBAction ?


Could try to clean build folder (option clean in Product menu).


Some comments on this code:


func names should begin with lowercase : func p2increaseCount, p2Add, …

I found the issue, It was the IBoutlets and IBactions were connected to things that didnt exist.