将值传递给另一个视图控制器并设置为等于输出时,我不断收到解包错误? (Swift 3.0)
I keep getting an unwrapping error when passing value to another view controller and set equal to output? (Swift 3.0)
当我尝试将我的函数 return 值设置为标签输出时,我不断收到此错误。
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.
}
*/
}
我正在尝试将数字传递给此视图控制器,然后将函数调用到 return 字符串(我正在制作剪刀石头布)。但是,当我设置输出 = 函数时,它给了我一个解包错误。
我确保我的连接良好。
然后我把这个值传给另一个storyboard,把output
的默认值设置为"value"
。
当我将字符串传递到输出时,它发生了变化。
也谢谢楼上的各位。 ^
当我尝试将我的函数 return 值设置为标签输出时,我不断收到此错误。
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.
}
*/
}
我正在尝试将数字传递给此视图控制器,然后将函数调用到 return 字符串(我正在制作剪刀石头布)。但是,当我设置输出 = 函数时,它给了我一个解包错误。
我确保我的连接良好。
然后我把这个值传给另一个storyboard,把output
的默认值设置为"value"
。
当我将字符串传递到输出时,它发生了变化。
也谢谢楼上的各位。 ^