How do I call the element of a struct in an array? #28
-
iOS Lesson 3
I thought of trying to call an element in the struct (eg. Classmate.name), however it does not work.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Hi @ndgsghdj, var index = 0
...
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = "\(ClassmatesArray[index].name)"
ageLabel.text = "\(ClassmatesArray[index].age)"
} Hope this helps! |
Beta Was this translation helpful? Give feedback.
Hi @ndgsghdj,
Classmate.name
wouldn't work because you have declared the Classmate struct type that contains two properties: name as well as age. You have then created multiple instances of the struct in the array, ClassmatesArray, so in order to access and read the values of the properties, you would then need to specify an index within the array.An example would be:
Hope this helps!