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

fatal error: unexpectedly found nil while unwrapping an Optional value

//
//  PlayList.swift
//  Algoryrhythm
//
//  Created by david on 3/09/15.
//  Copyright (c) 2015 Tomcorp. All rights reserved.
//

import Foundation
import UIKit
//data model

// llamar cada uno de los items
struct PlayList {
    var title: String?
    var description: String?
    var icon: UIImage?
    var largeIcon: UIImage?
    var artist: [String] = []
    var backgroundColor: UIColor = UIColor.clearColor()

    //inicializador de musiclibrary
    init(index: Int) {
        let musicLibrary = MusicLibrary().library
        let playlistDictionary = musicLibrary[index]
       // llamr cada item del otro view
        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)
        artist += playlistDictionary["artist"] as! [String]  //HERE ITS THE PROBLEM someone can help???? i have to put each as with ! symbol because xcode tell me to do it 
        let colorsDictionary = playlistDictionary["backgroundColor"] as! [String: CGFloat]
        backgroundColor = rgbColorFromDictionary(colorsDictionary)
    }


    func rgbColorFromDictionary(colorDictoniary: [String: CGFloat]) -> UIColor {
        let red = colorDictoniary["red"]!
        let green = colorDictoniary["green"]!
        let blue = colorDictoniary["blue"]!
        let alpha = colorDictoniary["alpha"]!

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


    }

}

Edited for syntax formatting.

2 Answers

Hava you tried as! String! and not an array?

yes and doesnt work as a string instead of a array

look these its my code and nothing happend also i upgrade xcode and appears a new issue that sais that ios simulator doesnt have ios9 simulator i allready download all

these its my code and error: fatal error: unexpectedly found nil while unwrapping an Optional value

// // PlayList.swift // Algoryrhythm // // Created by david on 3/09/15. // Copyright (c) 2015 Tomcorp. All rights reserved. //

import Foundation import UIKit //data model

// llamar cada uno de los items struct PlayList { var title: String? var description: String? var icon: UIImage? var largeIcon: UIImage? var artist: [String] = [] var backgroundColor: UIColor = UIColor.clearColor()

//inicializador de musiclibrary
init(index: Int) {
    let musicLibrary = MusicLibrary().library
    let playlistDictionary = musicLibrary[index]
   // llamr cada item del otro view
    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)
    artist += playlistDictionary["artist"] as! [String]! // IN THESE PART ITS THE ERROR
    let colorsDictionary = playlistDictionary["backgroundColor"] as! [String: CGFloat]
    backgroundColor = rgbColorFromDictionary(colorsDictionary)
}


func rgbColorFromDictionary(colorDictoniary: [String: CGFloat]) -> UIColor {
    let red = colorDictoniary["red"]!
    let green = colorDictoniary["green"]!
    let blue = colorDictoniary["blue"]!
    let alpha = colorDictoniary["alpha"]!

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


}

}