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 Build a Playlist Browser with Swift Building the Music Library and Playlist Models Using a Playlist Instance

Carldon King
seal-mask
.a{fill-rule:evenodd;}techdegree
Carldon King
iOS Development Techdegree Student 5,651 Points

Question about the rgbColorFromDictionary method

I tried something and I think it worked. I don't see any reason that I would get bugs, and my question is would this be an alternative, bad code, or is creating the function more efficient. Instead of using the method in the video, I typed this in right after the "artists +=..." array line, within the init() method:

let backgroundColorsDictionary = playlistDictionary["backgroundColor"] as! [String: CGFloat]

    let red = backgroundColorsDictionary["red"]
    let green = backgroundColorsDictionary["green"]
    let blue = backgroundColorsDictionary["blue"]
    let alpha = backgroundColorsDictionary["alpha"]

    backgroundColor = UIColor(red: red!/255.0, green: green!/255.0, blue: blue!/255.0, alpha: alpha!)

It worked and gave no compile errors. I just wanted to finish creating the Playlist struct on my own without watching the video and this was the conclusion I came to, but when I watched the video he created the method and I wanted to know did I just find another way to do something or am I just incorrect and need to learn why doing it this way is a mistake.

3 Answers

thomas lotocki
thomas lotocki
3,230 Points

Just to clarify is your code:

let backgroundColorsDictionary = playlistDictionary["backgroundColor"] as! [String: CGFloat]

let red = backgroundColorsDictionary["red"]
let green = backgroundColorsDictionary["green"]
let blue = backgroundColorsDictionary["blue"]
let alpha = backgroundColorsDictionary["alpha"]

backgroundColor = UIColor(red: red!/255.0, green: green!/255.0, blue: blue!/255.0, alpha: alpha!)

or is it only:

let backgroundColorsDictionary = playlistDictionary["backgroundColor"] as! [String: CGFloat]

thomas lotocki
thomas lotocki
3,230 Points

Caldron,

My knowledge is very basic as you can see from my progress bar! My understanding is that it works all right but since you are doing it right in the constant definition and not as a function you will not be able to call this function later on when you pass the instance of Playlist. You may not need it ever and it may work but i guess doing it as a func gives you more flexibility later on.