About "Declaration is only valid at file scope"

IosSwiftDelegates

Ios Problem Overview


I have a class and extension Swift file. After adding a delegate that I declared in another file to the class, Xcode shows this error

> Declaration is only valid at file scope

at the extension line. I don't know what the problem is.

Can anyone help me to fix it?

class ListViewController: UIViewController, AddItemViewControllerDelegate {...}

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

}

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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

Solution 3 - Ios

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)
    }
}

Solution 4 - Ios

Make sure your class and extension are seperated.

class ViewController: UIViewController {}

extension name: type {}

Solution 5 - Ios

I had my extension calls at the bottom of my file and put them at the top and that fixed it for me. At the bottom, they were outside the class scope so I was a little stumped and just tried this.

Solution 6 - Ios

The extension should be out of the Class.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDennisView Question on Stackoverflow
Solution 1 - IosNate CookView Answer on Stackoverflow
Solution 2 - IosJordanView Answer on Stackoverflow
Solution 3 - IospolarwareView Answer on Stackoverflow
Solution 4 - IosSiddharth ProthiaView Answer on Stackoverflow
Solution 5 - IosJohn MarkhamView Answer on Stackoverflow
Solution 6 - IosEmmanuel CuevasView Answer on Stackoverflow