Objective-C const 和 let Swift 在 运行 和编译时间方面的差异

Differences Objective-C const and let Swift in terms of run and compile time

我想找出 Objective-C 和 Swift 常量之间的区别。我刚刚看了一个教程文档

//Objective-C
const int number = 0;
//Swift
let number = 0

"A const is a variable initialized at compile time with a value or an expression that must be resolved at compilation time. An immutable created with let is a constant determined at runtime. You can initialize it with a static or a dynamic expression. This allows a declaration such as:

let higherNumber = number + 5

Note that you can only assign its value once."

有人可以解释这些短语并深入研究一下为什么让常量在运行时确定吗?我在想 let 是不变的,永远不会改变。

let 表示一旦赋值就不能更改。编译器将强制执行。像“let now=Date()”这样的表达式无法在编译时确定,但在编译时您可以禁止重新分配给“now”。“let pi = 3.141”可以在编译时确定,从而实现优化,并且就像 C 的 const

引用类型的注意事项(类) let 意味着分配的对象将继续是同一个对象,这并不意味着对象本身不能或不会改变。