gpt4 book ai didi

ios - 如何在swift中通过NSManagedObjectContext更新核心日期中的对象?

转载 作者:行者123 更新时间:2023-11-29 02:25:10 25 4
gpt4 key购买 nike

我从核心数据中获取对象数组:

lazy var managedObjectContext : NSManagedObjectContext? = {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
if let managedObjectContext = appDelegate.managedObjectContext {
return managedObjectContext
}
else {
return nil}
}()
var tests=[Test]()
func fetchLog() {
let fetchRequest = NSFetchRequest(entityName: "Test")
if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [Test] {
tests = fetchResults
}

然后托盘更新它:

 managedObjectContext?.updatedObjects(tests[atRow] as NSManagedObject)

但是出现错误:'(NSManagedObject) -> $T5' 与'NSSet' 不相同

怎么了?

最佳答案

NSManagedObject 已注册到 NSManagedObjectContext(MOC)。要更新它,请调用 MOC 的 save()

像这样:

let aTest = test[atRow] as Test
// update through properties like aTest.id = 2
...

// and save
var error: NSError?
if managedObjectContext!.save(&error) {
println("saved successfully")
} else {
println("failed to save")
if let saveError = error {
println("error=\(saveError.localizedDescription)")
}
}

关于ios - 如何在swift中通过NSManagedObjectContext更新核心日期中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27652756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com