我正在从该站点http://www.appcoda.com/introduction-to-core-data/跟踪有关核心数据的教程。
但是我收到以下错误:
2016-06-23 17:55:11.905 MyStore [6020:596233]-[NSAsynchronousFetchResult mutableCopyWithZone:]:无法识别的选择器已发送到实例0x7f8950e12eb0
我放置了一个断点,并且错误似乎来自以下过程。
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//Fetch the devices from the persistent store.
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
self.devices = [[managedObjectContext executeRequest:fetchRequest error:nil] mutableCopy]; //error here
[self.tableView reloadData];
}
我将属性“ devices”声明为NSMutableArray。
任何类型的帮助将不胜感激。
因此,当我遵循该过程时,我使用的方法不正确。
我应该使用的方法是executeFetchRequest,并且错误地使用了executeRequest。第一个确实返回一个NSArray,但另一个返回一个NSPersistentStoreResult。
这里是方法。
- (nullable NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error;
- (nullable __kindof NSPersistentStoreResult *)executeRequest:(NSPersistentStoreRequest*)request error:(NSError **)error NS_AVAILABLE(10_10, 8_0);
由于沃洛我的帮助是能够检查我的对象返回确实是我所期待的类型是不从那里我可能已经改变了方法,但后来我发现,返回的对象是一个更为复杂的之一,并有一个我可以使用的NSArray属性。
所以这是我的测试代码:)
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//Fetch the devices from the persistent store.
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
id whatAreYou = [managedObjectContext executeRequest:fetchRequest error:nil];
NSLog(@"%@", [whatAreYou class]); // turns out you are a NSPersistentStoreResult
// lucky for me you are have finalResult property that returns an NSArray. :)
self.devices = [[whatAreYou finalResult] mutableCopy]; //no more errors here :)
[self.tableView reloadData];
}
非常感谢您的帮助。从现在开始,我将添加到调试工具中来检查对象类型:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句