Swift: label text --> “fatal error: unexpectedl

2019-02-21 16:21发布

问题:

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"

        }
    }

回答1:

I have tried this code and it working fine for me:

class ViewController: UIViewController {

var player : Int = Int()      //Declare this globally

@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"

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

@IBAction func rockButton(rockbut: UIButton) {
     player = 0
    var ai = arc4random_uniform(3)
    self.playerChoice.text = "You: Rock"
}



@IBAction func paperButton(sender: UIButton) {
     player = 1
    var ai = arc4random_uniform(3)
    self.playerChoice.text = "You: Paper"
}

@IBAction func scissorsButton(sender: UIButton) {
     player  = 2
    var ai = arc4random_uniform(3)
    self.playerChoice.text = "You: Scissors"
}



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    var p : String = "Undecided"

    if (player == 0) {
        var p: String = "Rock"
    } else if (player == 1) {
        var p: String = "Paper"
    } else if (player == 2) {
        var p: String = "Scissors"
    }


    }

}


回答2:

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.


回答3:

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:

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



回答4:

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



回答5:

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.



标签: swift label null