恒定属性在swift中意味着什么?

时间:2017-07-06 12:29:58

标签: swift

我正在学习SWIFT。我在读书时不懂一句话。下面的句子是什么意思?:

  

“向ViewController.swift添加一个名为elementList的常量属性   并使用以下元素名称对其进行初始化:let elementList =   [" Carbon"," Gold"," Chlorine"," Sodium"]“   这是否意味着创建新类或我必须创建结构?

2 个答案:

答案 0 :(得分:0)

在您的情况下,您正在创建一个字符串数组并将其存储到名为elementList的常量变量中。当您使用let创建此变量时,意味着无法在之后更改该值。所以你不能以这种方式声明这个数组之后添加或删除元素等等

答案 1 :(得分:0)

class ViewController: UIViewController {
    var intValue = 1 //This is a property, but it is variable. That means its value can be changed in future.
    let doubleValue = 3.14 // This is a property too, but it is constant. That means its value can't be change
    // Both `intValue` & `doubleValue` will be in memory till ViewController's existence.

}

在您的情况下:

let elementList = ["Carbon", "Gold", "Chlorine", "Sodium"]

elementListString数组,因为let关键字表示它是 常量属性 < /强>

  

将名为elementList的常量属性添加到ViewController.swift并初始化它。 这样看起来很像

class ViewController: UIViewController {
    let elementList = ["Carbon", "Gold", "Chlorine", "Sodium"]

    //..
}