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

lochie westfall
lochie westfall
2,568 Points

My playlist browser app crashes because of two lines

this is my masterViewController ---------------------------------------------

import UIKit

class PlaylistMasterViewController: UIViewController {

@IBOutlet weak var aButton: UIButton!
@IBOutlet weak var playlistImageView0: UIImageView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    aButton.setTitle("Press Me", forState: .Normal)

    let playlist = Playlist(index: 0)
    playlistImageView0.image = playlist.icon
}

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


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showPlaylistDetail" {
        let playlistDetailController = segue.destinationViewController as PlaylistDetailViewController
        playlistDetailController.playlist = Playlist(index: 0)
    }
}

}

this is my playlists ---------------------------------------------------

// // Playlist.swift // Algorhythym // // Created by Lochie Westfall on 21/03/2015. // Copyright (c) 2015 Lochie Westfall. All rights reserved. //

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/255.0)
}

}

this is my detail view -------------------------------------------------------------

import UIKit

class PlaylistDetailViewController: UIViewController {

var playlist: Playlist?

@IBOutlet weak var playlistTitle: UILabel!
@IBOutlet weak var playlistDescription: UILabel!
@IBOutlet weak var playlistCoverImage: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    if playlist != nil {
        playlistCoverImage.image = playlist!.largeIcon
        playlistCoverImage.backgroundColor = playlist!.backgroundColor

if i comment out this it works

        playlistTitle.text = playlist!.title 
        playlistDescription.text = playlist!.description 

    }




}

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

}

3 Answers

Hi!

When I tried your code it worked fine once I force downcast all of the values in the initialization for the Playlist Struct by using as! instead of as

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)


    }

let me know if that works