Blooky Blooky - 7 months ago 30
Swift Question

Why do I get: Consecutive declarations on a line must be separated by ";" - Xcode (Swift)

I keep getting the error: Consecutive declarations on a line must be separated by ";" on the last line of my code. I do not know where I have made my mistake. Adding in a ";" does not work. This is in my ViewController.swift file.

Here is my code:

import UIKit

class ViewController: UIViewController {

@IBOutlet var scoreLabel: UILabel!
@IBOutlet var timerLabel: UILabel!
var count = 0
var seconds = 0
var timer = NSTimer()

@IBAction func buttonPressed() {
count++
scoreLabel.text = "Score \n\(count)"


func setupGame() {
seconds = 30
count = 0

timerLabel.text = "Time: \(seconds)"
scoreLabel.text = "Score: \(count)"
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("subtractTime"), userInfo: nil, repeats: true)
}

func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupGame()
}

func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func subtractTime() {
seconds--
timerLabel.text = "Time: \(seconds)"

if(seconds == 0) {
timer.invalidate()
}
}


Any help is Greatly appreciated!!!

Answer

You forgot to add "}" - closing bracket in buttonPressed function.

Change it like this :

import UIKit
class ViewController: UIViewController
{

    @IBOutlet var scoreLabel: UILabel!
    @IBOutlet var timerLabel: UILabel!
    var count = 0
    var seconds = 0
    var timer = NSTimer()

    @IBAction func buttonPressed() {
        count++
        scoreLabel.text = "Score \n\(count)"
    } // This one was missed

    func setupGame() {
        seconds = 30
        count = 0

        timerLabel.text = "Time: \(seconds)"
        scoreLabel.text = "Score: \(count)"
        timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("subtractTime"), userInfo: nil, repeats: true)
    }

    func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setupGame()
    }

    func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func subtractTime() {
        seconds--
        timerLabel.text = "Time: \(seconds)"

        if(seconds == 0)  {
            timer.invalidate()
        }
    }
}