Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: label text --> "fatal error: unexpectedly found nil while unwrapping an Optional value"

Tags:

null

swift

label

like it says in the title, i am trying to change label text upon click of a button. Error appears at line self.playerChoice.text = "You: Rock"

import UIKit

class ViewController: UIViewController {

var player : Int = Int()

@IBOutlet weak var readyLabel: UILabel!

@IBAction func noButton(sender: AnyObject) {
    exit(0)
}

// ---------------------------------------------------------

@IBOutlet var computerChoice: UILabel!

@IBOutlet var playerChoice: UILabel!

@IBOutlet var score: UILabel!


// Variables -------------------------------------------------

let Rock : String = "Rock"
let Paper : String = "Paper"
let Scissors : String = "Scissors"

//------------------------------------------------------------



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

    }


// ----------------------------------------------------------------

@IBAction func rockButton(rockbut: UIButton) {

    player = 0
    var ai = arc4random_uniform(3)
    self.playerChoice.text = "You: Rock"

    }

@IBAction func paperButton(paperbut: UIButton) {

    player = 1
    var ai = arc4random_uniform(3)
    self.playerChoice.text = "You: Paper"

    }

@IBAction func scissorsButton(scissorsbut: UIButton) {

    player = 2
    var ai = arc4random_uniform(3)
    self.playerChoice.text = "You: Scissors"

        }
    }
like image 939
artie711 Avatar asked Nov 24 '14 20:11

artie711


4 Answers

I ran into this problem and it turned out that the labels that I was trying to edit didn't exist at the time the code ran.

Turns out I was referencing the same view controller from a parent view and a child container view. The labels I was trying to change were only in the container view but as both views loaded it ran the view controller for both so it tried to find the labels that didn't exist in the parent view and threw the above error.

So the lesson I learned... If a reference to a view object is throwing a NIL..

  • The View isn't properly mapped to the View Controller.
  • The object within the view isn't mapped to the referencing IBOutlet.
  • Or as in my case, there are multiple views connected to the same View Controller and references to view objects in one view are not being found in the other.
like image 89
Christopher Wade Cantley Avatar answered Sep 22 '22 15:09

Christopher Wade Cantley


Looks like player choice is not initialized.

@IBOutlet var playerChoice: UILabel!

Maybe the connection between the outlet and InterfaceBuilder/Storyboard is lost. Try to connect it again.

I've created a small demo and everything works fine:

enter image description here

Check if the circles at the left side of your IBOutlet are filled. Otherwise the connection is lost.

like image 42
Stefan Avatar answered Sep 20 '22 15:09

Stefan


What fixed this for me (and it gets me everytime, especially when you are new to using storyboards) is to make sure that you are initializing your view controller like so :

slideShowVC = (UIStoryboard(name: "Main",bundle: nil).instantiateViewControllerWithIdentifier("WWPhotoSlideShowVC") as! WWPhotoSlideShowVC)

instead of the stand alone xib way :

slideShowVC = WWSlideShowVC()

Or else all your outlets will be nil na many headaches will soon follow.

like image 44
shokaveli Avatar answered Sep 18 '22 15:09

shokaveli


I ran into the same issue in Xcode 6.2. I use individual XIBs instead of storyboards. The problem for me was that with Swift, Xcode does not automatically associate the XIB with the view controller if the names are the same. Hence the IBOutlets for the labels are pointing to nil giving the fatal.

You can change the viewcontroller.xib to be called modulename.viewcontroller.xib so that xcode can associate it with the view controller and the issue goes away.

More options are mentioned on this thread:

Can't Load UIViewController XIB file in Storyboard in Swift

like image 39
peaxol Avatar answered Sep 18 '22 15:09

peaxol