Reputation: 624
For some reason if my score is greater than highScore it is not executing my function. Any help is much appreciated.
Here's my code:
import UIKit
import AVFoundation
struct Question {
var Question : String!
var Answers : [String]!
var Answer : Int!
}
class ViewController: UIViewController {
@IBOutlet weak var highScoreLbl: UILabel!
@IBOutlet var Buttons: [UIButton]!
@IBOutlet weak var QLabel: UILabel!
@IBOutlet weak var Label: UILabel!
@IBOutlet weak var incorrectLabel: UILabel!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var theEnd: UILabel!
@IBOutlet weak var continueButton: UIButton!
var scoreLbl = UILabel()
var score = Int()
var Questions = [Question]()
var QNumber = Int()
var AnswerNumber = Int()
var wrongAnswers = Int()
var highScore = 0
override func viewDidLoad() {
super.viewDidLoad()
Questions = [Question(Question: "What is the Biggest Hit of Bing Crosby?" , Answers: ["Swinging on a Star", "Now is the Hour", "White Christmas", "Beautiful Dreamer"], Answer: 2),
Question(Question: "What is Elvis Presely's Middle Name?", Answers: ["Aaron", "Micheal", "George", "Matthew"], Answer: 0),
Question(Question: "How Many Oscars did Titanic win?", Answers: ["5", "7", "10", "11"], Answer: 3),
Question(Question: "From which country did Pitta Bread originate?", Answers: ["Spain", "France", "Greece", "Russia"], Answer: 2),
Question(Question: "What is the largest living creature on Earth?", Answers: ["Whale", "Shark", "Sea Turtle", "Alligator"], Answer: 0),
Question(Question: "What does ATM stand for?", Answers: ["Automatic Treasure Machine", "Automatic Tax Machine", "Anti Tax Machine", "Automatic Teller Machine"], Answer: 3),
Question(Question: "What's the world's second largest French speaking city?", Answers: ["Paris", "Montreal", "Versailles", "Québec"], Answer: 1),
Question(Question: "What Country is the largest producer of Olive Oil?", Answers: ["Italy", "France", "Greece", "Spain"], Answer: 3),
Question(Question: "How long is the Great Wall of China?", Answers: ["3200 miles", "4000 miles", "2000 kilometers", "4500 miles"], Answer: 1),
Question(Question: "Who is on the 10 dollar bill?", Answers: ["George Washington", "Thomas Jefferson", "Alexander Hamilton", "John Adams" ], Answer: 2),
Question(Question: "How many World Series did Yogi Berra win as a player?", Answers: ["11", "10", "5", "7" ], Answer: 1),
Question(Question: "Which three countries hosted the Winter Olympics during the 1990's?", Answers: ["Norway, France, Russia", "US, Sweeden, Canada",
"Japan, Canada, Germany", "Slovenia, France, South Korea" ], Answer: 0),]
scoreLbl = UILabel(frame: CGRectMake(35, 45, 77, 45))
scoreLbl.textAlignment = NSTextAlignment.Center
scoreLbl.text = "-1"
self.view.addSubview(scoreLbl)
PickQuestions()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func PickQuestions(){
score++
scoreLbl.text = "\(score)"
if Questions.count > 0{
QNumber = random() % Questions.count
QLabel.text = Questions[QNumber].Question
AnswerNumber = Questions[QNumber].Answer
for i in 0..<Buttons.count{
Buttons[i].setTitle(Questions[QNumber].Answers[i], forState: UIControlState.Normal)
}
Questions.removeAtIndex(QNumber)
}
else{
theEnd.text = "You Win!"
theEnd.alpha = 1
}
incorrectLabel.alpha = 0
}
func saveHighScore(){
if score >= highScore {
highScore == score
highScoreLbl.text = "High Score: " + String(highScore)
}
else{}
}
@IBAction func Btn1(sender: AnyObject) {
if AnswerNumber == 0{
PickQuestions()
}
else{
incorrectLabel.text = "You are incorrect!"
incorrectLabel.alpha = 1
score--
scoreLbl.text = "\(score)"
}
}
@IBAction func Btn2(sender: AnyObject) {
if AnswerNumber == 1{
PickQuestions()
}
else{
incorrectLabel.text = "You are Incorrect!"
incorrectLabel.alpha = 1
score--
scoreLbl.text = "\(score)"
}
}
@IBAction func Btn3(sender: AnyObject) {
if AnswerNumber == 2{
PickQuestions()
}
else{
incorrectLabel.text = "You are Incorrect!"
incorrectLabel.alpha = 1
score--
scoreLbl.text = "\(score)"
}
}
@IBAction func Btn4(sender: AnyObject) {
if AnswerNumber == 3{
PickQuestions()
}
else{
incorrectLabel.text = "You are Incorrect!"
incorrectLabel.alpha = 1
score--
scoreLbl.text = "\(score)"
}
}
}
Upvotes: 0
Views: 88
Reputation: 16827
highScore == score
should be highScore = score
, otherwise highScore
is always 0
looks like saveHighScore
is never getting called also
func PickQuestions(){
score++
saveHighScore() <--- need to do things like this when score is updated
scoreLbl.text = "\(score)"
To save:
//How to save result, do this after the highscore is set
let defaults = NSUserDefaults.standardUserDefaults()
defaults["highScore"] = highScore
//How to read saved result, do this in your viewdidload
let defaults = NSUserDefaults.standardUserDefaults()
if let hs = defaults["highScore"]
{
highScore = hs
}
Now you may have to cast to the correct type to get this to work, but the basic idea is outlined above
Upvotes: 4