Mohamed Hassan
Mohamed Hassan

Reputation: 2477

Swift Error: Consecutive declarations on a line must be separated by ';'

This is my code:

import UIKit

class PlaylistMasterViewController: UIViewController {

@IBOutlet weak var abutton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    abutton.setTitle("Press me!", forState: .Normal)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func prepareForSegue(segue: UIStoryboardSegue, sender:     AnyObject?) {
    if segue.identifier ==  "showPlaylistDetailSegue" {
        let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController

        playlistDetailController.segueLabelText = "Yay!You Pressed"
}


}

I am getting error on the last line! my xCode version is 6.3.2 .. and I can't overcomer this because the app is always failing. I am getting here two errors: 1- Consecutive declarations on a line must be separated by ';' 2- Expected Declaration

Thanks in advance

Upvotes: 0

Views: 3863

Answers (2)

Justin Rose
Justin Rose

Reputation: 1041

You're missing your ending bracket to close up your class, I have demonstrated this by adding a comment every time a bracket has opened and closed, something like:

if myVar == 1 { //OPEN: 1
  //do something
} //CLOSE: 1

The 1 is simply a means to keep track of which bracket is which, so if you had multiple brackets (which you do), you'd see something like

//OPEN: 2 and //CLOSE: 2

I have added your code below showing you when your brackets have opened and closed.

import UIKit

class PlaylistMasterViewController: UIViewController { //OPEN: 1

  @IBOutlet weak var abutton: UIButton!

  override func viewDidLoad() { //OPEN: 2
    super.viewDidLoad()
    abutton.setTitle("Press me!", forState: .Normal)
  } //CLOSE: 2 -- 1 still needs to be closed

  override func didReceiveMemoryWarning() { //OPEN: 3
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  } //CLOSE: 3 -- 1 still needs to be closed

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { //OPEN: 4
    if segue.identifier ==  "showPlaylistDetailSegue" { //OPEN: 5
      let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
      playlistDetailController.segueLabelText = "Yay!You Pressed"
    } //CLOSE: 5 -- 1 & 4 still need to be closed
  } //CLOSE: 4 -- 1 still needs to be closed

// This is where you need to close 1, you're missing the bracket under this comment

} //ADD ME -- CLOSE: 1 -- no brackets to close left

I hope this is a little clearer to you now. Please practice properly indenting your code, and it will save a lot of time debugging on simple errors like this! :)

Upvotes: 1

David Berry
David Berry

Reputation: 41226

As @MartinR points out, you're missing a closing brace.

The "Re-indent" menu item (Editor/Structure/Re-Indent or ^I) can be used to easily automatically format the current select, which makes such problems painfully hard to ignore.

Upvotes: 0

Related Questions