Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,650 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!