我尝试从coredata中删除一个与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)'
。有人知道我能如何解决这个错误吗?
UPDATE:
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
是的第一个终于工作。非常感谢你的帮助! – paro
@paro请接受答案,并注意 – agy
我标记为正确的。如果我想为一个字符串变量替换'Test2',你能告诉我要写什么吗? '(格式:“name ='\(variable)'”)'不起作用。 – paro