为什么这个标签没有显示正确的值? Swift
Why is this Label not showing the correct value? Swift
我编写了一个代码来从我的 CoreDate 实体中获取数据,以将最高整数显示为 Highscore 标签中的值。我不明白为什么它不起作用?我试过带或不带额外功能...
func loadHighscore() {
//Kontext identifizieren
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
//Anfrage stellen
let context = appDelegate.persistentContainer.viewContext
let entityName = "PushUps"
let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
do {
let results = try context.fetch(request)
for result in results {
guard let count = (result as! NSManagedObject).value(forKey: "highScore") as? Int16 else {
return
}
}
if count > highScore {
highscoreLabel.text = "Highscore: \(count)"
highScoreChanged(newHighScore: Int16(count))
// Console statement:
print("New Highscore: \(count)")
}
} catch {
print("error")
}
}
func highScoreChanged(newHighScore: Int16) {
highscoreLabel.text = "Highscore: \(newHighScore)"
}
}
务必在主队列中执行标签更新。否则可能无法完成。
你的做法有点奇怪
更好的方法是加载按 highScore
降序排列的数据,因此第一项是具有最高值的项。
强烈建议利用通用核心数据类型并使用点符号而不是 KVC value(forKey
func loadHighscore() {
//Kontext identifizieren
// delegate can be forced unwrapped. The app doesn't even launch if AppDelegate doesn't exist
let appDelegate = UIApplication.shared.delegate as! AppDelegate
//Anfrage stellen
let context = appDelegate.persistentContainer.viewContext
let entityName = "PushUps"
// Use a specific fetch request
let request = NSFetchRequest<PushUps>(entityName: entityName)
// add a sort descriptor to sort the items by highScore descending
request.sortDescriptors = [NSSortDescriptor(key: "highScore", ascending: false)]
do {
// results is an array of PushUps instances, no type cast needed
let results = try context.fetch(request)
if let result = results.first, result.highScore > highScore {
highScore = result.highScore
print("New Highscore: \(highScore)")
}
} catch {
print(error)
}
highscoreLabel.text = "Highscore: \(highScore)"
}
函数highScoreChanged
也不需要。如果保存的高分高于当前值 (属性 highScore
),则更新 属性,并在方法结束时使用 [=24] 的值更新文本字段=] highScore
.
我编写了一个代码来从我的 CoreDate 实体中获取数据,以将最高整数显示为 Highscore 标签中的值。我不明白为什么它不起作用?我试过带或不带额外功能...
func loadHighscore() {
//Kontext identifizieren
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
//Anfrage stellen
let context = appDelegate.persistentContainer.viewContext
let entityName = "PushUps"
let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
do {
let results = try context.fetch(request)
for result in results {
guard let count = (result as! NSManagedObject).value(forKey: "highScore") as? Int16 else {
return
}
}
if count > highScore {
highscoreLabel.text = "Highscore: \(count)"
highScoreChanged(newHighScore: Int16(count))
// Console statement:
print("New Highscore: \(count)")
}
} catch {
print("error")
}
}
func highScoreChanged(newHighScore: Int16) {
highscoreLabel.text = "Highscore: \(newHighScore)"
}
}
务必在主队列中执行标签更新。否则可能无法完成。
你的做法有点奇怪
更好的方法是加载按 highScore
降序排列的数据,因此第一项是具有最高值的项。
强烈建议利用通用核心数据类型并使用点符号而不是 KVC value(forKey
func loadHighscore() {
//Kontext identifizieren
// delegate can be forced unwrapped. The app doesn't even launch if AppDelegate doesn't exist
let appDelegate = UIApplication.shared.delegate as! AppDelegate
//Anfrage stellen
let context = appDelegate.persistentContainer.viewContext
let entityName = "PushUps"
// Use a specific fetch request
let request = NSFetchRequest<PushUps>(entityName: entityName)
// add a sort descriptor to sort the items by highScore descending
request.sortDescriptors = [NSSortDescriptor(key: "highScore", ascending: false)]
do {
// results is an array of PushUps instances, no type cast needed
let results = try context.fetch(request)
if let result = results.first, result.highScore > highScore {
highScore = result.highScore
print("New Highscore: \(highScore)")
}
} catch {
print(error)
}
highscoreLabel.text = "Highscore: \(highScore)"
}
函数highScoreChanged
也不需要。如果保存的高分高于当前值 (属性 highScore
),则更新 属性,并在方法结束时使用 [=24] 的值更新文本字段=] highScore
.