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

Will Matthews
Will Matthews
8,127 Points

Why are we defining the MusicLibrary and Playlist structs in two separate files?

I'm confused by why we're defining these two structs in separate files? I can understand moving data model code out of the controller code. But given that the definition of Playlist is tightly integrated with the dictionary elements defined in MusicLibrary, why not define the two structs in the same file?

4 Answers

The MusicLibrary struct contains the details for ALL the playlists. So when accessing the playlists we would have one more layer of a struct to go through.

Creating a separate struct allows our ViewController to access a struct with only the relevant data. This allows your code to be more broken up in stages.

Will Matthews
Will Matthews
8,127 Points

Thanks for the reply Madhav. I can totally appreciate why we're defining things in two different structs, but what I'm curious about is why these two structs are set up in separate files. From what I can figure, there's very little portability of the Playlist struct without the prior definition of this particular MusicLibrary struct. So in my opinion, it would make more sense to include them both in the same file rather than to store them in two different files, no?

Sorry I misread your question.

You can see that you can still fully use the Playlist struct without including the MusicLibrary file. So, I assume that separating them allows you to in certain cases restrict your access to MusicLibrary, and two separate files grants you that luxury.

Will Matthews
Will Matthews
8,127 Points

I actually don't think you can use this particular Playlist struct as it is defined without our MusicLibrary struct file. It won't be able to initialize. That why I think in this particular case, I'd feel more comfortable defining the two in the same file.

I think the structs were separated in two files just because of MVC best practices.

Will Matthews
Will Matthews
8,127 Points

I guess what I'm asking is how is this a best practice? I totally understand why you want to store the data model apart from the view controller code. No problem there. But why is each individual struct stored in two different files when this Playlist struct cannot be initialized without the definition of this MusicLibrary struct. Why would they not be stored together since the Playlist can't be used without MusicLibrary? ie. Create a file called MusicDataModel or some-such-thing and the store both in there?