About "Declaration is only valid at file scope"

Make sure that the extension is declared at the end of your main class and after the last curly braces "}"

class ListViewController: UIViewController, AddItemViewControllerDelegate {
   //Make sure that everything is clean here! 
}

extension ListViewController: UITableViewDataSource{
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        performSegueWithIdentifier("ShowDetail", sender: indexPath)
    }
}

The extension must be at the root level - don't embed them into a class or whatever.


The error is somewhere in your ... — that error means that your ListViewController class didn't get closed, so the extension is being interpreted as nested inside, like this:

class ListViewController {
    ...
    extension ListViewController {
    }
}

Find the missing closing brace and you should solve the problem.


Make sure your class and extension are seperated.

class ViewController: UIViewController {}

extension name: type {}