简体   繁体   English

属性“public”只能在非本地范围内使用

[英]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. 我将Cordova添加到Swift项目中。

I added a Bridging Header file and the Cordova build dependencies, and I did get autocomplete working (the Cordva CDV classes were auto-completing). 我添加了一个Bridging Header文件和Cordova构建依赖项,我确实得到了自动完成工作(Cordva CDV类是自动完成的)。

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. 我的许多功能也停止了工作。

在此输入图像描述

Any suggestions as to what happened or what I could do to fix would be much appreciated 关于发生了什么或我能做些什么来修复的任何建议都将非常感激

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. 我同意Nate Cook对问题的分析,但是我的编译器抛出了这个错误,因为我在文件中错过了一个大括号( } )。 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 var URLRequest: NSURLRequest行上抛出错误:

public enum MyEnum: SomeProtocol {
    var someVariable {
        switch self {
        case .first:
            return something
        default:
            return default
        }
    // <------- needs brace here
    public var URLRequest: NSURLRequest {
        // Code here.
    }
}

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. 如果在嵌套在函数内的类型上声明了public ,或者在该上下文中声明的方法类型只具有局部作用域,则会出现该错误,因此无法将其标记为public。 Example: 例:

func foo() {
    public struct Bar {        
    }
}
// Attribute 'public' can only be used in a non-local scope

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM