核心数据错误 - NSDate localizedCaseInsensitiveCompare:发送到实例的无法识别的选择器

Gab*_*iel 1 core-data objective-c avfoundation ios unrecognized-selector

我搜索过去几个小时但尚未找到答案.我实现了一个AVFoundation摄像头,我将图像数据保存到磁盘并仅存储核心数据中的路径.一切正常,但在随机拍摄照片后,我收到此错误:

CoreData:错误:严重的应用程序错误.在Core Data更改处理期间捕获到异常.这通常是NSManagedObjectContextObjectsDidChangeNotification的观察者中的错误. - [__ NSDate localizedCaseInsensitiveCompare:]:无法识别的选择器发送到实例

这是我的获取请求代码:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photo"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"notebook = %@", notebookItem];
[request setPredicate:predicate];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"dateTaken"
                                                              ascending:NO
                                                               selector:@selector(compare:)]];
Run Code Online (Sandbox Code Playgroud)

以下是我将数据保存在单独的类中的方法:

//I create a photo Object to save to core data and set properties like dateTaken and tittle

//Here is where I create the path name
NSDate *dateString = [[NSDate alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSString *path = [formatter stringFromDate:dateString];

//Now I save to disk
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:path];
NSLog(@"path: %@", path);
[[[self captureManager] ImageDataObject] writeToFile:filePath atomically:YES];

photo.imagePath = filePath;

[notebookItem addPhotosObject:photo];
[self.SharedDocumentHandlerInstance saveDocument];
Run Code Online (Sandbox Code Playgroud)

我追踪崩溃点,它出现在我保存文档的代码行中(上面的最后一个)但是获取请求可能是导致它的那个.我还在我的表中设置了self.fetchedResultsController.delegate = nil.再次,在拍摄随机数量的照片后会发生此错误.在此先感谢您的帮助!

and*_*der 6

您的问题是您无法呼叫localizedCaseInsensitiveCompare:NSDate.你需要改变它compare:.检查通过苹果的文档NSDate.

该方法localizedCaseInsensitiveCompare:需要NSString(Apple文档).

这个SO问题也可能有所帮助.

此网页中的表1还创建和使用排序描述符