I have made some progress on my app to play RoShambo, but am stumped on one particular thing. In one ViewController, I have established two properties of the class. I want them to be integers because I use a switch statement later on in the class with integers. However, I get an error when I use integers saying:
"Class 'ResultsViewController' has no initializers"
"stored property 'your play' without initial value prevents synthesized initializers"
Now, those errors go away if I make my stored properties optionals, but then I get errors on my switch statement because it uses integers, not optionals.
So I have two questions: 1) In the switch statement I have below, how would I use values of type "Int?" in a switch statement?
2) If my optional values are nil, how could I end the program and not conduct the switch statement, since it wouldn't make sense to conduct the comparison?
import Foundation
import UIKit
class ResultsViewController: UIViewController {
// MARK: Properties
var opponentPlay: Int?
var yourPlay: Int?
//Mark: Outlets
@IBOutlet weak var MatchResult: UILabel!
@IBOutlet weak var PlayAgainButton: UIButton!
//Mark: Life Cycle
override func viewWillAppear(_ animated: Bool){
//unwrap optional properties
if let opponentPlay = opponentPlay {
print("good play")
} else {
print("opponentPlay is nil")
}
if let yourPlay = yourPlay {
print("good play")
} else {
print("opponentPlay is nil")
}
switch (opponentPlay, yourPlay) {
case (1, 1), (1, 1), (2, 2), (2, 2), (3, 3), (3, 3):
self.MatchResult.text = "You Tie!"
case (1, 2):
self.MatchResult.text = "You Win!"
case (2, 1):
self.MatchResult.text = "You Lose!"
case (1, 3):
self.MatchResult.text = "You Lose!"
case (3, 1):
self.MatchResult.text = "You Win!"
case (2, 3):
self.MatchResult.text = "You Win!"
case (3, 2):
self.MatchResult.text = "You Lose!"
default:
break
}
Aucun commentaire:
Enregistrer un commentaire