swift - 在 Swift 中初始化类常量

标签 swift

我正在尝试做这样的事情(这是一个仅用于演示目的的人为示例):

class Test {
  let hello = "hello"
  let world = "world"
  let phrase: String {
    return self.hello + self.world
  }
}

但是你不能在 Swift 中使用 let 计算属性。有没有一种方法可以做到这一点而不必编写 init() 方法?谢谢!

最佳答案

let 对只读计算属性不起作用的原因是因为它用于声明属性的实际 在设置后永远不会改变 – 不是该属性是只读的。作为Apple docs说(强调我的):

You must declare computed properties — including read-only computed properties — as variable properties with the var keyword, because their value is not fixed. The let keyword is only used for constant properties, to indicate that their values cannot be changed once they are set as part of instance initialization.

因此,您需要使用 var 来反射(reflect)计算属性的值可能随时更改的事实,因为您是在访问它时即时创建它的。尽管在您的代码中,这不可能发生——因为您的 helloworld 属性本身就是 let 常量。但是,Swift 无法推断出这一点,因此您仍然必须使用 var

例如:

class Test {
    let hello = "hello"
    let world = "world"
    var phrase: String {
        return self.hello + self.world
    }
}

(这不会改变属性的可读性——因为您没有为它提供 setter ,它仍然是只读的)

但是在您的情况下,您可能需要考虑使用 lazy property相反,因为您的 helloworld 属性是常量。惰性属性在首次访问时创建,并在其剩余生命周期内保持其值 - 这意味着您不必在每次访问它时都将两个常量连接在一起。

例如:

class Test {
    let hello = "hello"
    let world = "world"
    lazy var phrase: String = {
        return self.hello + self.world
    }()
}

let 属性的另一个特点是它们的值在初始化之前应该总是已知的。因为在此之前可能不知道惰性属性的值,所以您还需要将其定义为 var


如果您仍然坚持要为此使用 let 属性,那么据我所知,您有两个选择。

第一个是最简洁的(虽然你说过你不想这样做)——你可以在初始化程序中分配你的 phrase 属性。只要在 super.init 调用之前执行此操作,就不必处理可选值。例如:

class Test {
    let hello = "hello"
    let world = "world"
    let phrase: String

    init() {
        phrase = hello+world
    }
}

您根本无法内联,因为该范围内的 self 指的是静态类,而不是该类的实例。因此,您无法访问实例成员,必须使用 init() 或惰性/计算属性。

第二个选项非常 hacky——您可以在类级别镜像您的 helloworld 属性,因此您可以在您的 phrase< 中内联访问它们声明。例如:

class Test {
    static let hello = "hello"
    static let world = "world"

    // for some reason, Swift has trouble inferring the type
    // of the static mirrored versions of these properties
    let hello:String = Test.hello
    let world:String = Test.world

    let phrase = hello+world
}

如果您实际上不需要您的 helloworld 属性作为实例属性,那么您可以将它们设为 static – 这将解决您的问题。

关于swift - 在 Swift 中初始化类常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161238/

相关文章:

ios - App Share 扩展网络丢失

ios - CurrentValueSubject 和 @Published 之间的区别

ios - 单元格中的破坏约束

ios - 向右滑动即可关闭 ViewController,无需使用 Storyboard Swift

ios - 在带有静态单元格的 Storyboard 中的 UITableView 中使用带有 XIB 的自定义单元格时, socket 为零

ios - 如何将文本从 SafariViewController 快速获取到我们的应用程序?

ios - 使用 UIImageView 在 UIView 中切割透明孔

ios - 打开ViewController(它是tabBarController中的navigationController)并运行函数

ios - 扩展属性(如果不可用)

arrays - 你能有一个在索引 0 处没有元素的数组吗