Skip to content

How do I call the element of a struct in an array? #28

Answered by granwyntan
ndgsghdj asked this question in Q&A - iOS
Discussion options

You must be logged in to vote

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:

var index = 0
...
override func viewDidLoad() {
    super.viewDidLoad()
    nameLabel.text = "\(ClassmatesArray[index].name)"
    ageLabel.text = "\(ClassmatesArray[index].age)"
}

Hope this helps!

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@granwyntan
Comment options

@ndgsghdj
Comment options

@granwyntan
Comment options

@ndgsghdj
Comment options

@granwyntan
Comment options

Answer selected by granwyntan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants