Ios 将高分复制到可读变量以显示在标签中
我的高分系统需要帮助。到目前为止,我制作的Ios 将高分复制到可读变量以显示在标签中,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我的高分系统需要帮助。到目前为止,我制作的receivedHighScore变量(见下文)显示在标签上。如果我硬编码它,它会工作并显示值,但我无法让它显示retrievedHighScore。即使我尝试创建一个全局变量并在显示它时使用它,我也会得到 如果有人能帮我把当前的高分计数器保存到score.HighScore或任何需要的地方,那就太棒了 目前我有两个部分。HighScore.swift,其中包括: import Foundation class HighScore: NSObject
receivedHighScore
变量(见下文)显示在标签上。如果我硬编码它,它会工作并显示值,但我无法让它显示retrievedHighScore
。即使我尝试创建一个全局变量并在显示它时使用它,我也会得到
如果有人能帮我把当前的高分计数器
保存到score.HighScore或任何需要的地方,那就太棒了
目前我有两个部分。HighScore.swift,其中包括:
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)
}
}
在我的GameViewController中,我有:
var Score = HighScore()
var receivedHighScore = HighScore()
override func viewDidLoad() {
super.viewDidLoad()
SaveHighScore().ArchiveHighScore(highScore: Score)
var retrievedHighScore = SaveHighScore().RetrieveHighScore() as HighScore
println(retrievedHighScore)
receivedHighScore = SaveHighScore().RetrieveHighScore() as HighScore
}
func update() {
labelCounter.text = String(counter++)
var highScoreYes = counter
Score.highScore = highScoreYes-1
}
当您通过以下方式获得高分时:
var retrievedHighScore = SaveHighScore().RetrieveHighScore() as HighScore
RetrievedHighScore设置为HighScore对象的实例
要获取highscore值,您需要访问RetrievedHighScore具有的highscore属性:
let currentHighScore: Int=retrivedHighScore.highScore