Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Why a separate struct to access the information from MusicLibrary? We could just pull them from directly from M.Library?

The init methods in the Playlist struct look complicating. Why exactly do we need them? Couldn't we just pull the information from MusicLibrary directly?

In addition, why did we initialize everything in Playlist struct with an init method? Don't we initialize them previously by:

 var title: String?
    var description: String?
    var icon: UIImage?
    var largeIcon: UIImage?
    var artists: [String] = []
    var backgroundColor: UIColor = UIColor.clearColor()

1 Answer

We could, but then we would have to pass all the music library with all of its playlists between controllers. Since in the example, there were 6-7 playlists that may not seem like a problem but with real data it would be. Another reason would be, most of the time, the data won't be hard coded in the app. We will pull it from a database, or from a json file so you'd be designing just the playlist struct to handle the data in your app.

Lastly, the code you are mentioning is just the declaration of the stored properties with only two of them being given initial values. The actual values of the struct are initialized within the init method.