-6

I am learning SWIFT. I don't understand one sentence while reading book. What does the sentence below means?:

“Add a constant property called elementList to ViewController.swift and initialize it with the following element names: let elementList = ["Carbon", "Gold", "Chlorine", "Sodium"]” does it mean create new class or I have to create struct?

Soulmaster
  • 101
  • 1
  • 9

2 Answers2

-1

In you situation, you are creating an array of string and store it into a constant variable called elementList. When you use let to create this variable, it means that the value cannot be changed afterwords. So you cannot add or remove element after declaring this array in this way, etc

Fangming
  • 24,551
  • 6
  • 100
  • 90
-1
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.

}

IN YOUR CASE:

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

elementList is a array of String, since let keyword denotes that it is a constant property

To Add a constant property called elementList to ViewController.swift and initialize it. IT WOULD LOOK SOMETHING LIKE THIS

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

    //..
}
aashish tamsya
  • 4,903
  • 3
  • 23
  • 34