当前正在学习Core Data,我需要将现有对象的值增加1。该属性存储为Int16()
,因此在调用context.save()
之前,我将其包装在该值周围我所拥有的是我可以调整该值,但好像它没有被更新。不确定是什么问题。
request.predicate = NSPredicate(format: "courseID = %d AND lessonID = %d", Int16(course), Int16(lesson))
request.fetchLimit = 1
if let attempts = try? context.fetch(request) {
print("Attempts: \(attempts)")
if attempts.count > 0 {
let attempt = attempts[0]
print("Current lesson attempt: \(attempt.attempt)")
attempt.attempt += Int16(1)
do {
print("Current lesson attempt: \(attempt.attempt)")
try context.save()
completion(true, nil)
} catch {
print("\(error)")
completion(false, AppError.logAttempt)
}
}
}
我尝试调整核心数据模型中的值(每次删除内容和设置)。所有打印语句正确输出所需的值,只是不将尝试值更新为1。
“尝试次数”属性设置为:Integer16(可选)和“使用标量”类型。
谢谢!