我是iOS-Swift的新手,消息:"失败!再次播放"在score < 3
时不显示。
func discussScore(sender: AnyObject){
// after asking all questions, if score >=20, the player accesses stage 2
if (questionField.text == listOfQuestions[4]) {
if (score >= 3) {
//go to next step
}
if (score < 3) {
alertMessageWrong("Fail! Play Again") // (here's my problem)
}
}
}
func alertMessageWrong(theMessage2: String) {
let alertController = UIAlertController(title: " ", message:theMessage2, preferredStyle: UIAlertControllerStyle.Alert)
alertController.view.tintColor = UIColor.redColor()
alertController.addAction(UIAlertAction(title:theMessage2, style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
首先,我在此行放置一个断点:
if (questionField.text == listOfQuestions[4])
并逐步穿过每条线,看看哪条路线被采取......您可以在此处了解如何调试。
要检查您是否击中了正确的问题,最好使用"step"整数而不是比较字符串。在回答每个问题后递增此整数。然后,您可以检查您是否在最后一个问题上执行以下操作:
if currentStep >= listOfQuestions.count { ... }
其次,如果您这样做,代码会更干净:
let minimumScore = 3
if score < minimumScore {
showAlert()
return
}
// go to next step.