1

I'm a code beginner, actually I've joined this world a few weeks ago. I'm trying to build up my first app for Ios by using Xcode and Swift. I want to create different table views and passing data between these tables.

Now, I made up the code, but I keep getting an "Expected Declaration" error. I really don't understand how to fix it. Can someone please help me? Thanks.

//
//  ViewController.swift
//  Tot_Forum
//
//  Created by Fausto Saltetti on 18/07/16.
//  Copyright (c) 2016 Fausto Saltetti. All rights reserved.
//

import UIKit

class FirtTableViewController: UITableViewController {

var FirstTableArray = [String]()

var SecondArray = [SecondTable]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    FirstTableArray = ["Focus on learning", "Participate and network", "Access and build knowledge", "Assess, reflect, evaluate", "Inspire and generate ideas", "Problem solve and plan", "Map ideas and relationships"]

    SecondArray =
        [SecondTable(SecondTitle: ["After Action Review","Audience Response Systems","Blogs","Case Studies", "Discussion Forums UPDATE","Jigsaw","Peer Assist", "Podcasting","Presentations", "Role Play", "Screencasting", "Social Networking", "Sociometrics"]),
            SecondTable(SecondTitle: ["After Action Review","Audience Response Systems","Blogs","Case Studies", "Discussion Forums UPDATE","Jigsaw","Peer Assist", "Podcasting","Presentations", "Role Play", "Screencasting", "Social Networking", "Sociometrics"]),
            SecondTable(SecondTitle: ["After Action Review","Audience Response Systems","Blogs","Case Studies", "Discussion Forums UPDATE","Jigsaw","Peer Assist", "Podcasting","Presentations", "Role Play", "Screencasting", "Social Networking", "Sociometrics"])]

}

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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
    return FirstTableArray.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    Cell.textLabel?.text = FirstTableArray[indexPath.row]

    return Cell

     func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()!

        var DestViewController = segue.destinationViewController as! SecondTableViewController

        var SecondTableArrayTwo : SecondTable

        SecondTableArrayTwo = SecondArray[indexPath.row]

        DestViewController.SecondArray = SecondTableArrayTwo.SecondTitle
        }

}   **//Error here: !Expected declaration**
1
  • closing brace "}" is missing for your class Commented Jul 18, 2016 at 8:08

2 Answers 2

1

mistake here is your prepareForSegue function inside of a cellForRowAtIndexPath function.

they should be in the global scope

var SecondTableArrayTwo : SecondTable? should be unwrapped or optional with its init.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    Cell.textLabel?.text = FirstTableArray[indexPath.row]
    return Cell
}  

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()!
        var DestViewController = segue.destinationViewController as! SecondTableViewController
        var SecondTableArrayTwo : SecondTable?
        SecondTableArrayTwo = SecondArray[indexPath.row]
        DestViewController.SecondArray = SecondTableArrayTwo!.SecondTitle
}
Sign up to request clarification or add additional context in comments.

1 Comment

can I ask to help me a little more by writing the correct code? Or is the one you typed, I've copied and past but it shows me the same mistake.
1

Starting with the error you are getting, "Expected Declaration" error is for the missing brace "}" of your class.

Solving this error will give you another warning because code written after a return statement never get executed. You have written prepareForSegue function after the return statement.

Now the main issue, prepareForSegue must be in global scope as @Özgür Ersil mentioned in his answer. Thus add a closing brace for your class. Remove the prepareForSegue function from the cellForRowAtIndexPath function and write it in global scope inside your class.

Hope this will help you :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.