I have an array of objects which I want to store it in dictionary based on student id. In my dictionary my key is of type String and value is an array of string. How do I append the values in the array?
My student array:
var studentArray = [Student(id: '1', subject: 'History'), Student(id: '2', subject: 'History'), Student(id:'1', subject: 'Maths')]
My final dictionary should be like:
{'1': ['History', 'Maths'], '2': ['History']}
My code:
var dictionary = [String, [String]]()
for student in studentArray {
dictionary.updateValue(student.subject, forKey: student.id)
}
This gives me output as:
{'1': ['Maths'], '2': ['History']}
I tried: dictionary[student.id].append([student.subject])
but this gives me nil output.
How do I append the value to an array inside the dictionary?