2017-07-06 69 views
-5

我正在學習SWIFT。看書時我不明白一句話。什麼是一句裝置下方?:不變屬性意味着什麼?

「一個不變的性質稱爲元素列表添加到ViewController.swift 並用以下元素名稱初始化:讓元素列表= [‘低碳’,‘黃金’,「氯「,」鈉「]」 這是否意味着創建新的類或我必須創建結構?

回答

0

在您的情況下,您正在創建一個字符串數組並將其存儲到一個名爲elementList的常量變量中。當您使用let創建此變量時,這意味着該值不能在後綴中更改。所以,你不能添加或以這種方式宣佈此陣後刪除元素等

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"] 

    //.. 
} 
+0

我知道,但爲什麼它被寫入到元素列表ViewController.swift? – Soulmaster

+0

ViewController.swift會是一個包含ViewController類的swift文件。我也添加了答案。 –