使用 NSUserDefaults 保存高分 [英] Saving highscores with NSUserDefaults

查看:43
本文介绍了使用 NSUserDefaults 保存高分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存游戏的最高分.我正在尝试通过 NSUserDefaults 来做到这一点.这是我正在使用的代码:

I'm trying to save the highscore of my game. I'm trying to do this via NSUserDefaults. This is the code I'm using:

//To save highest score
var highestScore:Int = 20
NSUserDefaults.standardUserDefaults().setObject(highestScore, forKey:"HighestScore")
NSUserDefaults.standardUserDefaults().synchronize()

//To get the saved score
var savedScore: Int = NSUserDefaults.standardUserDefaults().objectForKey("HighestScore") as Int
println(savedScore)

但是我收到一个错误,NSUserDefaults 说预期声明",我不知道如何正确实现它.或者我应该为此使用 NSArchiver 吗?如果是这种情况,我该如何实施?

But I get an error with NSUserDefaults saying "Expected declaration" and I can't figure out how to properly implement this. Or should I be using NSArchiver for this? And if that is the case how could I implement this?

推荐答案

使用 NSCoding.创建一个 Swift 文件HighScore"

Use NSCoding. Create a Swift file "HighScore"

import Foundation

class HighScore: NSObject {

    var highScore: Int = 0

    func encodeWithCoder(aCoder: NSCoder!) {
           aCoder.encodeInteger(highScore, forKey: "highScore")
    }

    init(coder aDecoder: NSCoder!) {
        highScore = aDecoder.decodeIntegerForKey("highScore")
    }

    override init() {
    }
}

class SaveHighScore:NSObject {

    var documentDirectories:NSArray = []
    var documentDirectory:String = ""
    var path:String = ""

    func ArchiveHighScore(#highScore: HighScore) {
        documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        documentDirectory = documentDirectories.objectAtIndex(0) as String
        path = documentDirectory.stringByAppendingPathComponent("highScore.archive")

        if NSKeyedArchiver.archiveRootObject(highScore, toFile: path) {
            println("Success writing to file!")
        } else {
            println("Unable to write to file!")
        }
    }

    func RetrieveHighScore() -> NSObject {
        var dataToRetrieve = HighScore()
        documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        documentDirectory = documentDirectories.objectAtIndex(0) as String
        path = documentDirectory.stringByAppendingPathComponent("highScore.archive")
        if let dataToRetrieve2 = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? HighScore {
            dataToRetrieve = dataToRetrieve2
        }
        return(dataToRetrieve)
    }
}

然后对于您的 ViewController:

Then for your ViewController:

   import UIKit


    class ViewController: UIViewController, UITextFieldDelegate {
        var Score = HighScore()

        override func viewDidLoad() {
            super.viewDidLoad()

        Score.highScore = 100
        SaveHighScore().ArchiveHighScore(highScore: Score)
        var retrievedHighScore = SaveHighScore().RetrieveHighScore() as HighScore
        println(retrievedHighScore.highScore)

        }
    }

这篇关于使用 NSUserDefaults 保存高分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆