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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28205651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:33:28  来源:igfitidea点击:

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

xcodecordovaswiftcompiler-errors

提问by Aggressor

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

我添加了一个桥接头文件和 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.

我的项目到处都是错误。我的很多功能也停止工作了。

enter image description here

在此处输入图片说明

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

关于发生的事情或我可以做些什么来解决的任何建议将不胜感激

采纳答案by Nate Cook

That error shows up when you have publicdeclared 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:

当您public对嵌套在函数或方法中的类型进行声明时,会出现该错误- 在该上下文中声明的类型只有局部范围,因此不能标记为公共。例子:

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

回答by kbpontius

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: NSURLRequestline:

我同意 Nate Cook 对问题的分析,但是我的编译器抛出了这个错误,因为我在文件中遗漏了一个大括号 ( })。例如,switch 中 default 语句后面的大括号丢失了。在这种情况下,它会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.
    }
}