来自 Apple 文档“Learn the Essentials of Swift”
A constant is a value that stays the same after it’s declared the first time, while a variable is a value that can change. A constant is referred to as immutable, meaning that it can’t be changed, and a variable is mutable. If you know that a value won’t need to be changed in your code, declare it as a constant instead of a variable.
但在 REPL 中,我可以这样做:
14> let favoriteNumber = 4
favoriteNumber: Int = 4
15> let favoriteNumber = 5
favoriteNumber: Int = 5
我显然遗漏了一些东西:这种差异是否与编译器或运行时有关,还是其他原因?
最佳答案
您可以在 REPL 中完成您在普通代码中无法完成的事情。
您不能在 playground 或编译代码中重新分配常量。
我不确定这是 REPL 中提供的特殊便利,还是一个错误。
关于ios - 如果常量是不可变的,为什么我可以使用 let 重新分配它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32341463/