About "Declaration is only valid at file scope"
Asked Answered
C

6

58

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

}
Chrystal answered 17/2, 2015 at 3:39 Comment(4)
Can you show the code that's producing this error?Yseulta
@0x7fffffff♦ yes sureChrystal
just in case, ensure that your extension is OUTSIDE your class! :)Indefeasible
The extension must be at the root level - don't embed them into a class or whatever.Chingchinghai
D
114

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.

Deweese answered 17/2, 2015 at 4:9 Comment(3)
Thanks for replying. I just double checked my code, the class is closed for sure. Or is it the matter of conforming protocol?Chrystal
Oh, right—perhaps the error message is just bad. That method actually belongs to UITableViewDelegate. If you want to conform to UITableViewDataSource, you need at least tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath:.Deweese
yeap my closing curly brace was in the incorrect position. thanksGerthagerti
C
22

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

Chingchinghai answered 8/2, 2016 at 7:23 Comment(0)
N
16

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)
    }
}
Neral answered 18/7, 2015 at 18:54 Comment(0)
A
4

Make sure your class and extension are seperated.

class ViewController: UIViewController {}

extension name: type {}
Achromatous answered 24/8, 2018 at 17:48 Comment(0)
R
0

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.

Rhapsodist answered 25/4, 2019 at 20:38 Comment(0)
A
0

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)
        }
Allene answered 18/12, 2020 at 0:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.