let declarations require an initializer expression
Solution 1
From the Swift Language Reference:
When a constant is declared at global scope, it must be initialized with a value.
You can only defer initialization of a constant in classes/structs, where you can choose to initialize it in the initializer of the class/struct.
The meaning of "The value of a constant doesn’t need to be known at compile time" refers to the value of the constant. In C/Objective-C a global constant needs to be assigned a value that can be computed by the compiler (usually a literal like 10
or @"Hello"
). The following would not be allowed in Objective-C:
static const int foo = 10; // OK
static const int bar = calculate_bar(); // Error: Initializer element is not a compile-time constant
In Swift you don't have this restriction:
let foo = 10 // OK
let bar = calculateBar(); // OK
Edit:
The following statement in the original answer is not correct:
You can only defer initialization of a constant in classes/structs, where you can choose to initialize it in the initializer of the class/struct.
The only place where you cannot defer is in global scope (i.e. top level let
expressions). While it's true that you can defer initialization in a class/struct, that's not the only place. The following is also legal for example:
func foo() {
let bar: Int
bar = 1
}
Solution 2
A constant does not need to be known at compile, but it must have a value after initialization:
class MyClass: NSObject {
let aConstant: Integer; // no value
init() {
aConstant = 4; // must have a value before calling super
super.init();
}
}
This allows you to set the constant to a value after it is declared and potentially unknown at compile time.
Solution 3
Answer for Swift 2:
You can write constants as follows:
let aConstant:Int
aConstant = 5
Setting the type this way means: "This will be constant and it will have value when you need it". Notice that you cannot use the constant before setting value to it, there is a compile time error:
Constant 'aConstant' used before being initialized
Furthermore you can set value to aConstant
only once. If you try to set value for second time, there is compile time error:
Immutable value 'aConstant' may only be initialized once
Anyway you cannot do this for global constants, there is compile time error:
Global 'let' declaration requires an initializer expression
Solution 4
the let
keyword, by definition, defines a constant
.
Thus, you can't modify it once its been set.
Since thats the case, they need to be initialized when they are declared!
The solution here is to do either:
let aConstant = 5
or change it to a var
var aNonConstant:Int
aNonConstant = 5
onmyway133
I work with iOS, macOS, Android, React, Electron and Nodejs. I actively work on open source with 1.3k+ followers on GitHub, 45k+ apps touched and 3.4m+ downloads on CocoaPods. I also write on Medium with 2.3k+ followers with 90k+ monthly views. Support my apps https://onmyway133.com/apps Open source https://github.com/onmyway133 Writing https://medium.com/@onmyway133
Updated on July 24, 2022Comments
-
onmyway133 almost 2 years
I'm reading The Swift Programming Language, in the Simple Values section
“Use let to make a constant and var to make a variable. The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once”
So I think I can do this
let aConstant:Int aConstant = 5
But I get let declarations require an initializer expression !!
Why is that ? What does they mean by "The value of a constant doesn’t need to be known at compile time" ?