ios - 如果常量是不可变的,为什么我可以使用 let 重新分配它们?

标签 ios swift constants immutability

来自 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

我显然遗漏了一些东西:这种差异是否与编译器或运行时有关,还是其他原因?

enter image description here

最佳答案

您可以在 REPL 中完成您在普通代码中无法完成的事情。

您不能在 playground 或编译代码中重新分配常量。

我不确定这是 REPL 中提供的特殊便利,还是一个错误。

关于ios - 如果常量是不可变的,为什么我可以使用 let 重新分配它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32341463/

相关文章:

ios - 从 Storyboard 获取 ViewController

ios - 如何使用 ReactiveCocoa 3 实现基本的 UITextField 输入 + UIButton 操作场景?

ios - fatal error : Found Nil While Unwrapping Optional Value TableViewCell to UIViewController

c++ - 如何用字符串文字初始化 const char []

ios - 如何快速访问 XCTest 的外部文件

ios - Xamarin 表单电话无法在 iOS 上运行

ios - 使用 iOS 7 向后边缘滑动时在哪里实现自动保存代码?

ios - 如何以编程方式使用 Swift 获取其他应用程序的崩溃日志

c++ - 这里声明了什么?

php - 何时以及如何在 PHP 中使用常量?