我正在关注此站点的 Core Data 教程 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);
感谢 Volodymyr 的帮助,我能够检查我返回的对象确实是我期望的类型,但不是,从那里我可以改变方法,但后来我发现返回的对象是一个更复杂的对象,并且有一个我可以使用的 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];
}
非常感谢您的帮助。从现在开始,我将检查对象类型添加到我的调试工具中:)
关于ios - 无法识别的选择器 mutableCopyWithZone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38002692/