Need help plz - Structured Gym Regimen - struc of struc with []?

Hello :slight_smile:

i don’t understand why we use […] for call a struct , in the lessons about structures , you can have a property of another struct ;

struct Pets {
var petDog: Dog // where Dog is another struct
var petCat: Cat
}

but in the “Structured Gym Regimen” project, you have this code in the lesson , which is working ;

struct Exercise {

var name: String

var muscleGroups: [String]

var reps: Int

var sets: Int

var totalReps: Int

init(name: String, muscleGroups: [String], reps: Int, sets: Int){

self.name = name

self.muscleGroups = muscleGroups

self.reps = reps

self.sets = sets

self.totalReps = reps * sets

}

}

var pushUp = Exercise(name: “Push up”, muscleGroups: [“Triceps”, “Chest”, “Shoulders”],

reps: 10, sets: 3)

print(pushUp)

struct Regimen {

var dayOfWeek: String

var exercises: [Exercise]

init(dayOfWeek: String, exercises: [Exercise]){

self.dayOfWeek = dayOfWeek

self.exercises = exercises

}

}

var mondayRegimen = Regimen(dayOfWeek: “Monday”, exercises:[pushUp])

//My question is why when you are creating the Regimen struc, you have to put [] when you are declaring his property var exercises: [Exercise]
you should juste write without […] like in the dog lesson ?

Because the exercises property is an array of exercises, which are of the Exercise type.

In fact, you did exactly this when creating the muscleGroups property for the Exercise struct:

The hint under Task 2 in this project explains how this is used:

As for the dog example, the petDog property will be one instance of the Dog type. If you had multiple dogs that you wanted to include in your Pets struct, you might put them in an array:

var petDogs: [Dog]
2 Likes

Thank you el_cocodrilo,
so in the dog exemple, var petDogs: [Dog]
the property is an array with the type between [.]
so when we’ll call or declare the petDogs property I could enter an array of instances of the type Dog.
I think I understand now :slight_smile:

2 Likes