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

Rehaan Advani
PLUS
Rehaan Advani
Courses Plus Student 2,485 Points

I'm getting a fatal error: unexpectedly found nil while unwrapping an optional value

import Foundation
import UIKit

class ViewController2: UIViewController {

    @IBOutlet weak var goBackToFirstViewController: UIButton!
    @IBOutlet weak var globeMap: UIImageView!
    @IBOutlet weak var displayInfoHeader: UILabel!
    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var animateImageView: UIButton!

    var imageList = [UIImage]()

    @IBAction func animateAction() {
        startAnimation()
    }

    @IBAction func hideAll(sender: AnyObject) {
        displayInfoHeader.hidden = true
        button.hidden = true
    }

    @IBAction func singaporeInformation(sender: AnyObject) {
        button.hidden = false
        displayInfoHeader.hidden = false
        displayInfoHeader.text = "Singapore, Singapore"
    }

    @IBAction func boiseInformation(sender: AnyObject) {
        button.hidden = false
        displayInfoHeader.hidden = false
        displayInfoHeader.text = "Boise, Idaho"
    }

    @IBAction func indiaInformation(sender: AnyObject) {
        button.hidden = false
        displayInfoHeader.hidden = false
        displayInfoHeader.text = "Mumbai, India"
    }

    @IBAction func paloAltoInformation(sender: AnyObject) {
        button.hidden = false
        displayInfoHeader.hidden = false
        displayInfoHeader.text = "Palo Alto, California"
    }

    override func viewDidLoad() {

        for i in 1...53 {
            let imageName = "\(i).png"
            imageList.append(UIImage(named: imageName)!) //THIS IS WHERE I AM GETTING MY ISSUE
        }

        button.hidden = true
        displayInfoHeader.hidden = true
        globeMap.image = UIImage(named: "map.png")
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func startAnimation() -> Void {
        if !globeMap.isAnimating() {
            globeMap.animationImages = imageList
            globeMap.startAnimating()
        } else {
            globeMap.stopAnimating()
            globeMap.image = UIImage(named: "map.png")
        }

    }
}

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Rehaan,

Make sure you have images in your xcassets folder that are numbered from 1 to 53 otherwise you will get an error as the executable won't be able to find a file named 1.png for example.

Happy coding!