从Coredata中删除一个对象(收到错误)

时间:2015-07-12 12:56:14

标签: ios swift core-data xcode6 nsmanagedobjectcontext

我尝试从与Test2匹配的coredata中删除对象。我试过这个:

    let context = self.fetchedResultsController.managedObjectContext
    let fetchRequest = NSFetchRequest(entityName:"Person")
    fetchRequest.predicate = NSPredicate(format: "name = Test2")
    var error : NSError?
    let results = managedObjectContext!.executeFetchRequest(fetchRequest, error:&error)

    context.deleteObject(results.firstObject as NSManagedObject)

我在这行代码中遇到错误:context.deleteObject(results.firstObject as NSManagedObject)。它说:Cannot invoke 'deleteObject' with an argument list of type '(NSManagedObject)'。有人知道如何解决这个错误吗?

更新:

2015-07-12 20:43:09.601 Note[4222:138125] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to generate SQL for predicate (name == Test2) (problem on RHS)'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010ebd6c65 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x0000000110741bb7 objc_exception_throw + 45
2   CoreData                            0x000000010e7f0bbc -[NSSQLGenerator newSQLStatementForRequest:ignoreInheritance:countOnly:nestingLevel:] + 1724
3   CoreData                            0x000000010e7dcdc4 -[NSSQLAdapter _statementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:] + 244
4   CoreData                            0x000000010e6f4e0c -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:] + 316
5   CoreData                            0x000000010e6f4a86 -[NSSQLCore newRowsForFetchPlan:] + 118
6   CoreData                            0x000000010e6f433c -[NSSQLCore objectsForFetchRequest:inContext:] + 524
7   CoreData                            0x000000010e6f3dbb -[NSSQLCore executeRequest:withContext:error:] + 299
8   CoreData                            0x000000010e7cea6c __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 3356
9   CoreData                            0x000000010e7d7c30 gutsOfBlockToNSPersistentStoreCoordinatorPerform + 192
10  libdispatch.dylib                   0x0000000111e65614 _dispatch_client_callout + 8
11  libdispatch.dylib                   0x0000000111e4b002 _dispatch_barrier_sync_f_invoke + 365
12  CoreData                            0x000000010e7c9245 _perform + 197
13  CoreData                            0x000000010e6f3a58 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 504
14  CoreData                            0x000000010e6f22ca -[NSManagedObjectContext executeFetchRequest:error:] + 586
15  Note                               0x000000010e4efb72 _TFC5Note14ViewController19deleteSelectedTasksfS0_FT_T_ + 738
16  Note                               0x000000010e4ef843 _TFC5Note14ViewController7delTaskfS0_FT_T_ + 51
17  Note                               0x000000010e4ef882 _TToFC5Note14ViewController7delTaskfS0_FT_T_ + 34
18  Foundation                          0x000000010f011744 __NSFireTimer + 83
19  CoreFoundation                      0x000000010eb3e174 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
20  CoreFoundation                      0x000000010eb3dd35 __CFRunLoopDoTimer + 1045
21  CoreFoundation                      0x000000010eaffd3d __CFRunLoopRun + 1901
22  CoreFoundation                      0x000000010eaff366 CFRunLoopRunSpecific + 470
23  GraphicsServices                    0x000000011128ca3e GSEventRunModal + 161
24  UIKit                               0x000000010f475900 UIApplicationMain + 1282
25  Note                                0x000000010e5146c7 main + 135
26  libdyld.dylib                       0x0000000111e99145 start + 1
27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

2 个答案:

答案 0 :(得分:0)

你可以使用context.deleteObject(results?.first as! NSManagedObject)它会编译但是如果它不是NSManagedObject它会崩溃。

这显然不是正确的做法。请记住:!是代码味道,因为在大多数情况下你不需要它们。

firstObject是一种NSArray方法,要使用它,您应指定results类型为NSArray(即let results: NSArray)。 Swift标准库等效项为first,并返回可选值,而deleteObject不接受可选项。所以你应该打开它,你可以同时检查它是否是NSManagedObject类型。

如下:

   if let results = context.executeFetchRequest(fetchRequest, error:&error),
      let managedObject = results.first as? NSManagedObject {
         context.deleteObject(managedObject)
   }

答案 1 :(得分:0)

问题似乎出现在谓词定义中。请尝试:

fetchRequest.predicate = NSPredicate(format: "name = 'Test2'")

fetchRequest.predicate = NSPredicate(format: "%K = %@", "name", "Test2")

如果要添加变量。

fetchRequest.predicate = NSPredicate(format: "%K = %@", "name", variable)