常量unassigned可选默认情况下不会为零 [英] Constant unassigned optional will not be nil by default
问题描述
到目前为止,我的理解:如果您定义一个可选变量而不分配任何值,则编译器将自动分配nil
My understanding so far : If you define an optional variable without assign any values, the compiler will automatically assign the nil
代码段:
Code Snippets:
A:
A :
var someOptional : Int? //Change to `let` to trigger error
var aDefaultValue = 42
var theAnswer = someOptional ?? aDefaultValue
上面的代码段工作正常,但是,当我将变量someOptional
更改为常量时,编译器大喊错误(请参见下图)
The code snippet above works fine, however, when I changed the variable someOptional
to a constant, the compiler yelled an error (Please see the attached figure below)
B:
B :
然后我尝试使用类似的代码来解决问题.
I then tried the similar codes to down to the problem.
var someOptional : Int?
print(someOptional)
尽管如此,它对于变量工作正常,而对于常量类型则失败.
Still, it works fine with variable while failed with the constant type.
结论:
如果您定义了一个可选的常量,那么您必须明确地指定nil.因为它看起来没用(为什么您需要一个常量选项并分配了nil
),所以如果编译器自动为您执行此操作,则可能会引入错误.
If you define a constant optional you have to assign the nil explicitly if you mean that. Because it looks useless (why do you need a constant option with assigned with nil
), if the compiler did that for you automatically it may introduce an error.
问题:
为什么编译器对var
声明的可选内容假定nil
,而不对let
声明的可选内容假定nil
?
Why does the compiler assume nil
for var
declared optionals but not for let
declared optionals?
推荐答案
是的
一个可选变量不需要手动初始化.如果您在填充之前阅读过它,则它确实包含nil
.
从Apple 文档
如果您在不提供默认值的情况下定义了一个可选变量,则该变量会自动为您[n]设置为nil [...]
If you define an optional variable without providing a default value, the variable is automatically set to nil for you [...]
另一方面,编译器确实会强迫您在读取可选常量(let
)之前手动对其进行初始化.
On the other hand the compiler does force you to manually initialize an Optional constant (let
) before you can read it.
与变量不同,常数的值一旦设置就无法更改.当您的代码被编译时,尝试这样做会报告为错误[...]
Unlike a variable, the value of a constant cannot be changed once it is set. Attempting to do so is reported as an error when your code is compiled [...]
为什么?
一个常数只能写一次.它不需要在初始化的同一行上发生,但必须在您读取它之前发生.
Why?
A constant can be written only once. It doesn't need to happened on the same line it is initialized but it must happened before your read it.
例如该代码可以正常工作
E.g. this code works fine
let num: Int?
num = 1
print(num)
但是,如果编译器在num
中放置了一个临时nil
值,则该常量将被写入两次.这与常量的概念背道而驰.
However if the compiler had put a temporary nil
value inside num
then the constant would have been wrote twice. Which is against the concept of constant.
let num: Int?
print(num) // nil ??? <- this can't work!
num = 1
print(num) // 1
另一个例子
此代码段可以正常工作
Another example
This code snippet works fine
func printArea(width: Int?, height:Int?) {
let area: Int?
if let width = width, height = height {
area = width * height
} else {
area = nil
}
print(area)
}
同样,如果编译器在area
中放置了一个临时nil
值,则...
Again, if the compiler had put a temporary nil
value inside area
then...
func printArea(width: Int?, height:Int?) {
let area: Int?
print(area) // not possible! area is going to change in a moment
if let width = width, height = height {
area = width * height
} else {
area = nil
}
print(area)
}
这篇关于常量unassigned可选默认情况下不会为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!