Search code examples
swiftnullfuncunwrap

I keep getting an unwrapping error when passing value to another view controller and set equal to output? (Swift 3.0)


enter image description here

I keep getting this error when I am trying to set my function return values to a label output.

enter image description here

import UIKit

class finalData: UIViewController {

var userNum: Int!
var computerNum: Int!

@IBOutlet var output: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    self.output.text = chooseWinner(userNum: userNum, computerNum: computerNum)
    // Do any additional setup after loading the view.


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func chooseWinner(userNum : Int, computerNum : Int) -> String {
    if userNum == computerNum {
        return "There is a tie"
    }else if userNum == 1 && computerNum == 2{
        return "You lost!"
    }else if userNum == 1 && computerNum == 3{
        return "You won!"
    }else if userNum == 2 && computerNum == 1{
        return "You won!"
    }
    else if userNum == 2 && computerNum == 3{
        return "You lost!"
    }else if userNum == 3 && computerNum == 1{
        return "You lost!"
    }
    else if userNum == 3 && computerNum == 2{
        return "You won!"
    }else{
        return "value"
    }
}

func findPlayerImage (firstImage: Int) -> UIImage{
    if userNum == 1{
        return #imageLiteral(resourceName: "black")
    }else if userNum == 2{
        return #imageLiteral(resourceName: "black")
    }else{
        return #imageLiteral(resourceName: "black")
    }
}
func findCompImage (secondImage: Int) -> UIImage{
    if computerNum == 1{
        return #imageLiteral(resourceName: "black")
    }else if computerNum == 2{
        return #imageLiteral(resourceName: "black")
    }else{
        return #imageLiteral(resourceName: "black")
    }
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

I am trying to pass the numbers to this view controller and then call the function to return a String (I am making Rock, Paper, Scissors). But, when I set the output = the function, it gives me a unwrapping error.


Solution

  • I made sure my connections were good.

    Then I passed the value to another storyboard and set the default value of output to "value". When I passed a string into output, it changed.

    Thank you everyone above as well. ^