我知道这类问题很多,但我的代码与其他问题不同(我已经看过了)。下面是我在Swift中的代码。
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
context = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "StartUp")
var request3 = NSFetchRequest(entityName: "Users")
request.returnsObjectsAsFaults = false
request3.returnsObjectsAsFaults = false
var results: NSArray = context.executeFetchRequest(request, error: nil)!
var results3: NSArray = context.executeFetchRequest(request3, error: nil)!
res = results [0] as NSManagedObject
res.setValue(newStart, forKey: "startController")
if (results3.count > 0) {
do {
let user = (results3 as NSArray).lastObject as Users // App crashes here
context.deleteObject(user)
} while (results3.count > 0)
}
context.save(nil)
我正在尝试从Core Data中删除一个对象。 Users
类已创建并具有所需的NSManagedObject。为什么会崩溃?