From the Swift Language Reference:
When a constant is declared in the global scope, it must be initialized with a value.
You can only delay initialization of the constant in classes / structures, where you can initialize it in the class / structure initializer.
The value "Constant value does not have to be known at compile time" refers to the constant value. In C / Objective-C, a global constant must be assigned a value that can be calculated by the compiler (usually literally, like 10 or @"Hello" ). Objective-C does not allow:
static const int foo = 10;
In Swift, you do not have this limitation:
let foo = 10 // OK let bar = calculateBar(); // OK
Edit:
The following statement in the original answer is incorrect:
You can only delay initialization of the constant in classes / structures, where you can initialize it in the class / structure initializer.
The only place you cannot set aside is on a global scale (i.e. higher levels of let ). Although it is true that you can defer initialization in a class / structure, this is not the only place. It is also legal, for example:
func foo() { let bar: Int bar = 1 }
source share