-2

I am writing code for an app with a menu and for some weird reason, Xcode is giving me a nil error. All help is appreciated.


//
//  ViewController.swift
//  Menu
//
//  Created by Programmer on 8/1/17.
//  Copyright © 2017 Programmer. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var LeadingConstr1: NSLayoutConstraint!
    @IBOutlet weak var MenuView1: UIView!

    @IBOutlet weak var LeadingConstr2: NSLayoutConstraint!
    @IBOutlet weak var MenuView2: UIView!

    var menushowing1 = false;
    var menushowing2 = false;

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        MenuView1.layer.shadowOpacity = 1
        MenuView1.layer.shadowRadius = 6
        MenuView2.layer.shadowOpacity = 1
        MenuView2.layer.shadowRadius = 6
    }

    @IBAction func MenuBtn(_ sender: Any) {
        if (menushowing1) {
            LeadingConstr1.constant = -140
        }
        else {
            LeadingConstr1.constant = 0
            UIView.animate(withDuration: 0.3, animations: {
                self.view.layoutIfNeeded()
            })

            view.layoutIfNeeded()
        }

        menushowing1 = !menushowing1
    }

    @IBAction func MenuBtn2(_ sender: Any) {
        if (menushowing2) {
            LeadingConstr2.constant = -140
        }
        else {
            LeadingConstr2.constant = 0
            UIView.animate(withDuration: 0.3, animations: {
                self.view.layoutIfNeeded()
            })

            view.layoutIfNeeded()
        }

        menushowing2 = !menushowing2
    }


}

Here is the error I am getting:

fatal error: unexpectedly found nil while unwrapping an Optional value 2017-08-03 07:18:40.647621-0400 Menu[383:53946] fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) –

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Programmer
  • 61
  • 1
  • 1
  • 8

1 Answers1

1

You have some outlet Menuwhich is not associated with any @IBOutlet Menu. Delete that connection from XIB/Storyboard or add the variable in your view controller.

Mohammad Sadiq
  • 5,070
  • 28
  • 29