Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attribute 'public' can only be used in a non-local scope

A very technical error here and Google turned up nothing on this.

I am adding Cordova to a Swift Project.

I added a Bridging Header file and the Cordova build dependencies, and I did get autocomplete working (the Cordva CDV classes were auto-completing).

Everything was working fine until I suddenly got this error:

Attribute 'public' can only be used in a non-local scope

And my project just lit up with errors everywhere. Also tons of my functions stopped working.

enter image description here

Any suggestions as to what happened or what I could do to fix would be much appreciated

like image 898
Aggressor Avatar asked Jan 29 '15 01:01

Aggressor


2 Answers

For future readers:

I agree with Nate Cook's analysis of the question, however my compiler was throwing this error because I was missing a curly brace (}) higher up in the file. For example, the curly brace after the default statement in the switch is missing. In this case it would throw the error on the public var URLRequest: NSURLRequest line:

public enum MyEnum: SomeProtocol {
    var someVariable {
        switch self {
        case .first:
            return something
        default:
            return default
        }
    // <------- needs brace here
    public var URLRequest: NSURLRequest {
        // Code here.
    }
}
like image 85
kbpontius Avatar answered Sep 21 '22 15:09

kbpontius


That error shows up when you have public declared on a type that is nested inside a function or method—types declared in that context have only local scope, and thus can't be marked as public. Example:

func foo() {
    public struct Bar {        
    }
}
// Attribute 'public' can only be used in a non-local scope
like image 23
Nate Cook Avatar answered Sep 21 '22 15:09

Nate Cook