Dylan wants to know what the ellipses do in a struct initializer!
What do the ellipsis (three dots) mean in this case?
Most times when you initialize a struct, you know how many variables you need to properly form it. For example an address struct will only have ONE postal code.
Variadic Parameters
However, there are times when you might initialize a struct variable with any number of values. For example, how many students in a maths class? Well, it's different for each maths class isn't it?
Paste the following code into Playgrounds and explore the variations.
// Paste into Playgrounds.
// Dylan! You ARE using Playgrounds, right?!
struct MathsClass {
var students: [String] // An array of Students
init(newStudents: String ...) { // Not sure how many students will be added?
students = newStudents // Populate the students var
}
}
// Initialize the algebra class with five students.
let algebra = MathsClass(newStudents: "Andrew", "Charles", "Anne", "William", "Edward")
// Initialize the calculus class with two students
let calculus = MathsClass(newStudents: "Galileo", "Newton")
algebra.students // Show the students in the algebra class
calculus.students // Show the students in the calculus class
In many languages these are called Variadic Parameters.
See-> Variadic Parameters