gpt4 book ai didi

Swift 3 在其定义模块之外覆盖非开放变量

转载 作者:可可西里 更新时间:2023-11-01 00:55:21 26 4
gpt4 key购买 nike

我将我的 swift 2 代码转换为 swift 3。然后我收到了这个错误。谁能帮我解决这个问题?

open override var formatKey: String { //overriding non-open var outside of its defining module
get {
if customFormatKey != nil {
return customFormatKey!
}
return String(describing: type(of: self)).components(separatedBy: ".").last!
}
}

最佳答案

根据 The Swift Programming Language 的访问控制部分:

Open access applies only to classes and class members, and it differs from public access as follows:

  • Classes with public access, or any more restrictive access level, can be subclassed only within the module where they’re defined.

  • Class members with public access, or any more restrictive access level, can be overridden by subclasses only within the module where they’re defined.

  • Open classes can be subclassed within the module where they’re defined, and within any module that imports the module where they’re defined.

  • Open class members can be overridden by subclasses within the module where they’re defined, and within any module that imports the module where they’re defined.

open 在 Swift 3 及更高版本中等同于 public 在 Swift 2 中。有关更多信息,请参阅 Swift Evolution proposal SE-0117 .

要解决此问题,请将 formatKey 的原始定义从 public 更改为 open,并使用 override var formatKey: String 进行覆盖{ ...

关于Swift 3 在其定义模块之外覆盖非开放变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50456330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com