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!

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 Building the Master and Detail Views Displaying Playlist Information

Sergey Petrov
Sergey Petrov
2,424 Points

I trying to modify an app and I have an error.

So, in my app I have six dictionaries and I have a problem with icon. fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

if main0 != nil { Detail0CoverImage.image = main0!.icon0 Detail0CoverImage.backgroundColor = main0!.backgroundColor0 Detail0Title.text = main0!.title0 Detail0Description.text = main0!.description0 at debug area I can see that description0 - is ok but icon0 with error (0x000 etc).

I checked my code twise but I can not find an error

struct Main0{ var title0: String? var description0: String? var icon0: UIImage? var backgroundColor0: UIColor = UIColor.clearColor() var needTo0: [String] = []

init(index: Int) {

/// ЧАСТЬ 0

let dataLibrary0 = DataLibrary().library0
let mainDictionary0 = dataLibrary0[index]
title0 = mainDictionary0["title"] as String!
description0 = mainDictionary0["description"] as String!

let iconName0 = mainDictionary0["icon"] as String!
icon0 = UIImage(named: iconName0)

needTo0 += mainDictionary0["needTo"] as [String]

let colorsDictionary0 = mainDictionary0["backgroundColor"] as [String: CGFloat]
backgroundColor0 = rgbColorFromDictionary0(colorsDictionary0)


Library looks like this:

struct DataLibrary { let library0 = [ [ "title": "Название 1 либ 1", "description": "Описание 1 либ 1", "icon": "ss_01.pdf", "backgroundColor": ["red": 255, "green": 204, "blue": 51, "alpha": 1.0], "needTo": ["Действие 1 либ 1"] ], etc

3 Answers

Sergey Petrov
Sergey Petrov
2,424 Points

The case turned out that I was sloppy create Outlets for title and coverImage. So I did it new one and everything is OK.

Sweet. Nice Job finding the error. :)

Try unwrapping the icon0. Where else do you have this in your code? Try icon0!

Sergey Petrov
Sergey Petrov
2,424 Points

Thank you!

I tried to "comment" this part of code:

/// Передача картинки-сигвея на место картинки мастера
//    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//        if segue.identifier == "showDetail0" {
//           let detail0ViewController = segue.destinationViewController as Detail0ViewController
//           detail0ViewController.main0 = Main0(index: 0)
//      }

at MasterViewController and I have no any errors. The second screen (DetailViewController) shows me Title with Description BUT there is no image and background color.

So, as I understand, problem in this part of code maybe?

Sergey Petrov
Sergey Petrov
2,424 Points

If I 'comment' this part image, background and title, so it sends me the description. If title, image or background color without 'comment' it has an error.