Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Playlist Browser with Swift Building the Music Library and Playlist Models Creating a Music Library

harleythomas
harleythomas
16,512 Points

Why do we encapsulate the array and dictionary inside of a struct?

Couldn't we just call the array "musicLibrary" and access it directly from the ViewControllers?

2 Answers

Jens Hagfeldt
Jens Hagfeldt
16,548 Points

I think the intention of encapsulating the array and dictionary inside of a struct was because Pasan wanted this stage to be more simple for us. Because with a struct we automatically get initializers for the array and dictionary handed to us. If we instead would create them in our class we would also have to write those initializers as well.

harleythomas
harleythomas
16,512 Points

Thank you, Jens. That makes perfect sense and I think you are probably right.

If you hold the array and dictionary inside the struct then any instance of that struct will have those properties. If the array or dictionary is outside then they are wholly separate which might not be what you want.

I've not done the course you're referring to so can't add any detail to that, but having a struct containing such items means you can have multiple instances, all with the same capabilities, but separated in their data-content.

I hope that helps.

Steve.