Sto scrivendo un gioco iOS in Swift e volevo includere un'etichetta di alto livello alla fine. Penso che la funzione di salvataggio sia corretta, ma quella di carico è quella che mi dà problemi. Ho già creato un soggetto ("BestScores") e gli attributi ("classicBestScoreTF"):CoreData Swift: come salvare e caricare i dati?
Per salvare il record:
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)"
}
}
Non funziona :(Si prega di aiutare
si potrebbe iniziare passando e valutare un oggetto errore durante il salvataggio. – vikingosegundo
https://www.youtube.com/watch?v=3IDfgATVqHw&feature=youtu.be – herby
Basta seguire [QUESTO] (http://jamesonquave.com/blog/core-data-in-swift-tutorial-part-1 /), ti fornirà linee guida passo passo. – Shardul