CoreData Swift: Wie speichere und lade ich Daten?
Ich schreibe ein iOS-Spiel in Swift und wollte am Ende ein Highscore-Label hinzufügen. Ich denke, dass die Speicherfunktion korrekt ist, aber das Laden ist das, was mir Probleme bereitet. Ich habe bereits eine Entität ("BestScores") und die Attribute ("classicBestScoreTF") erstellt:
Um den Highscore zu speichern:
var bestscore25 = 1000
var score: int
func savescore() {
var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var score25: AnyObject! = NSEntityDescription.insertNewObjectForEntityForName("BestScores", inManagedObjectContext: context) as NSManagedObject
score25.setValue(score, forKey: "classicBestScoreTF")
context.save(nil)
}
func loadscore() {
var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
bestScore25 = valueForKey("classicBestScoreTF") as Int
}
func endgame() {
if score > bestScore25 {
savescore()
loadscore()
bestScoreLabel.text = "Best Score: \(bestScore25)"
}
if score < bestscore {
loadscore()
bestScoreLabel.text = "Best Score: \(bestScore25)"
}
}
Es funktioniert nicht :( Bitte helfen Sie!