我想知道如何在我的测验游戏中保存用户进度。每次用户关闭应用程序时,它都会重置。这是我的代码:
import UIKit
import GoogleMobileAds
class ViewController: UIViewController, GADBannerViewDelegate {
//Place your instance variables here
let allQuestions = QuestionBank()
var pickedAnswer : Bool = false
var questionNumber : Int = 0
var score : Int = 0
let googleAdTestID: String = "ca-app-pub-3940256099942544/2934735716"
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var bannerView: GADBannerView!
@IBOutlet weak var progressLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion() )
bannerView.adUnitID = googleAdTestID
bannerView.rootViewController = self
bannerView.load(GADRequest())
nextQuestion()
}
@IBAction func skipButton(_ sender: Any) {
skip()
}
func skip() {
if score <= 4 {
}
else if score >= 5 {
questionNumber = questionNumber + 1
nextQuestion()
score = score - 5
scoreLabel.text = "Coins: \(score)"
}
}
@IBAction func answerPressed(_ sender: AnyObject) {
if sender.tag == 1 {
pickedAnswer = true
}
else if sender.tag == 2 {
pickedAnswer = false
}
checkAnswer()
questionNumber = questionNumber + 1
nextQuestion()
}
func updateUI() {
scoreLabel.text = "Coins: \(score)"
progressLabel.text = "Question Number: \(questionNumber + 1)"
}
func nextQuestion() {
if questionNumber <= 37 {
questionLabel.text = allQuestions.list[questionNumber].questionText
updateUI()
}
else {
let alert = UIAlertController(title: "Awesome", message: "You have finished all the questions!! Do you want to start over?", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default, handler: { (UIAlertAction) in
self.startOver()
})
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
}
}
func checkAnswer() {
let correctAnswer = allQuestions.list[questionNumber].answer
if correctAnswer == pickedAnswer {
print("Correct!")
ProgressHUD.showSuccess("Correct!!")
// varible += 1 same as varible = varible + 1
score += 1
}
else {
print("wrong!")
ProgressHUD.showError("Wrong!!")
}
}
func startOver() {
questionNumber = 0
nextQuestion()
score = 0
scoreLabel.text = "Score: 0"
}
}
答案 0 :(得分:1)
保存
// save necessory information using UserDefaults
// Add this code at your business logic
let database = UserDefaults.standard
let questionNumber = 2
database.set(questionNumber, forKey: "LAST_ANSWERED_QUESTION_NUMBER")
let score = 23
database.set(score, forKey: "QUIZ_SCORE")
let sync = database.synchronize()
if sync{
print("userdefaults - sync done")
}else{
print("userdefaults - failed to sync")
}
取
//access stored values from UserDefaults
// Add this code at your business logic
let database = UserDefaults.standard
if let lastAnswredQuestion = database.value(forKey: "LAST_ANSWERED_QUESTION_NUMBER") as? Int{
print("LAST_ANSWERED_QUESTION_NUMBER \(lastAnswredQuestion)")
}
if let scored = database.value(forKey: "QUIZ_SCORE") as? Int{
print("QUIZ_SCORE \(scored)")
}
注意:数据将一直存储到安装应用程序,如果用户删除应用程序,则整个数据将从UserDefaults消失。