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 Refactoring Our Code Tapping On Our Playlists

Danya Baron
Danya Baron
3,170 Points

Every icon brings me to the same playlist detail view - Playlist Browser in Swift

I have checked to see if I listed it as Playlist(index:0) instead of Playlist(index:index) but I can't seem to find any errors. Here is my prepareForSegue method:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showPlaylistDetailSegue" {
            let playlistImageView = sender!.view as! UIImageView
            if let index = playlistsArray.indexOf(playlistImageView) {
                let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
                playlistDetailController.playlist = Playlist(index: index)
            }
        }
    }

Here is all of the PlaylistMasterViewController code:

import UIKit

class PlaylistMasterViewController: UIViewController {

    var playlistsArray: [UIImageView] = []
    @IBOutlet weak var playlistImageView0: UIImageView!
    @IBOutlet weak var playlistImageView1: UIImageView!
    @IBOutlet weak var playlistImageView2: UIImageView!
    @IBOutlet weak var playlistImageView3: UIImageView!
    @IBOutlet weak var playlistImageView4: UIImageView!
    @IBOutlet weak var playlistImageView5: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        playlistsArray += [playlistImageView0, playlistImageView1, playlistImageView2, playlistImageView3, playlistImageView4, playlistImageView5]

        for index in 0..<playlistsArray.count {
            let playlist = Playlist(index: index)
            let playlistImageView = playlistsArray[index]

            playlistImageView.image = playlist.icon
            playlistImageView.backgroundColor = playlist.backgroundColor
        }


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showPlaylistDetailSegue" {
            let playlistImageView = sender!.view as! UIImageView
            if let index = playlistsArray.indexOf(playlistImageView) {
                let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
                playlistDetailController.playlist = Playlist(index: index)
            }
        }
    }


    @IBAction func showPlaylistDetail(sender: AnyObject) {

        performSegueWithIdentifier("showPlaylistDetailSegue", sender: sender)

    }
}

thanks!

peterwu2
peterwu2
3,083 Points

Hi Danya,

Can you post your codes on the Playlist struct file? Your PlaylistMasterViewController code matches mine, so I'm thinking the problem may be with the Playlist struct.

1 Answer

Danya Baron
Danya Baron
3,170 Points

@peterwu2 Sure! Here it is:

import Foundation
import UIKit

struct Playlist {

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

    init(index: Int) {
        let musicLibrary = MusicLibrary().library
        let playlistDictionary = musicLibrary[index]


        title = playlistDictionary["title"] as! String!
        description = playlistDictionary["description"] as! String!

        let iconName = playlistDictionary["icon"] as! String!
        icon = UIImage(named: iconName)


        let largeIconName = playlistDictionary["largeIcon"] as! String!
        largeIcon = UIImage(named: largeIconName)

        artists += playlistDictionary["artists"] as! [String]

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

        backgroundColor = rgbColorFromDictionary(colorsDictionary)
    }


    func rgbColorFromDictionary(colorDictionary: [String: CGFloat]) -> UIColor {

        let red = colorDictionary["red"]!
        let green = colorDictionary["green"]!
        let blue = colorDictionary["blue"]!
        let alpha = colorDictionary["alpha"]!

        return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha)


    }



}