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 Adding Multiple Playlists

maq
maq
1,297 Points

fatal error: unexpectedly found nil while unwrapping an Optional value

I know a few others have gotten a similar error but I can't find my answer in other posts. I've followed Pasan's code exactly and can't seem to find errors. Below is the posted code that Xcode says the error is in. It specifically highlights this:

playlistsArray += [playlistImageView0, playlistImageView1, playlistImageView2, playlistImageView3, playlistImageView4, playlistImageView5]

Rest of the function:

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
        }

    }

2 Answers

That could be a problem in various places!

Firstly, do your playlistImageViews all have an outlet in your Main Storyboard? To tell, go to the storyboard, open the right column/window in the editor. Top-right, there's 5 little icons - the one on the far right is an arrow in a circle; click that.

Under there you should see something like Outlet Connections and Referencing Outlets. Now, click each of your image views on the storyboard - does each one have a referencing out - a unique one for its ImageView number?

Try that first - and then we can look elsewhere!

Steve.

maq
maq
1,297 Points

Thank you Steve!!! I don't know why the playlistImageView outlets didn't connect when I first created them so I redid them and it works!

Awesome - glad that worked! :-)