xcode 无法构造“错误”,因为它没有可访问的初始值设定项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40525046/
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 09:34:42  来源:igfitidea点击:

'Error' cannot be constructed because it has no accessible initializers

xcodeswift3

提问by Glenn

Not sure if this is an error in my code or a glitch in XCode.

不确定这是我的代码中的错误还是 XCode 中的故障。

I have this class (simplified version for the sake of clarity)

我有这个类(为了清楚起见的简化版本)

public class Error {

    let errors: [ (title: String, message: String)] =
        [("Some error title","Some error message"),
         ("Another error title", "Another error message") 
        ]

    var errorNo : Int

    init (_ errorNo: Int) {

        self.errorNo = errorNo
    }

    func title () -> String {
        return self.errors[self.errorNo].title
    }

    func message () -> String {
        return self.errors[self.errorNo].message
    }
}

In another class I have

在另一堂课我有

if someCondition {
    return Error (0)
}

Now the strange thing... Everything compiles and runs but if I let XCode sit idle for a few moments (not the fastest computer I'm using), XCode is giving me the infamous red dots (with exclamation marks) with the error:

现在奇怪的是......一切都编译并运行,但如果我让 XCode 闲置一会儿(不是我使用的最快的计算机),XCode 会给我臭名昭著的红点(带有感叹号)并显示错误:

'Error' cannot be constructed because it has no accessible initialisers

无法构造“错误”,因为它没有可访问的初始化程序

next to each time I do Error(0) (whatever Int I use in the constructor)

每次我做 Error(0) (无论我在构造函数中使用什么 Int )

I can compile and run again and the errors dissappear and then reappear

我可以再次编译并运行,错误消失然后重新出现

Using XCode version 8.1 (8B62)

使用 XCode 8.1 (8B62)

******** SEE COMMENTS **** additional info ********

******** 查看评论 **** 附加信息 ********

Still one (similar problem now after renaming Error to AppError)

还是一个(现在将Error重命名为AppError后出现类似问题)

func doSomething (blah: Int, test : String) -> AppError {

    some code
    return AppError(1)
}

It compiles and runs but after some time an error pops up next to func doSomething

它编译并运行,但一段时间后在 func doSomething 旁边弹出一个错误

Use of undeclared type 'AppError'

使用未声明的类型“AppError”

回答by Kerstin

Error is a Swift protocol and therefore has no accessible initialisers. It is possible your compiler is confusing Swift.Error with your local definition of Error. When referring to your Error type to avoid confusion you should include the namespace i.e.

Error 是一个 Swift 协议,因此没有可访问的初始化程序。您的编译器可能将 Swift.Error 与您本地的 Error 定义混淆了。当提到你的错误类型以避免混淆时,你应该包括命名空间,即

(Target Name).Error

(目标名称)。错误

Regarding the errors you have seen after renaming your class to AppError, there is an XCode bug where it displays old errors after the app compiles and runs, as long as it is compiling and running you can ignore these errors.

关于您在将类重命名为 AppError 后看到的错误,有一个 XCode 错误,它在应用程序编译和运行后显示旧错误,只要它正在编译和运行,您就可以忽略这些错误。