Ios 一对多关系核心数据:在prepareForSegue中获取:
在过去的几个小时里,我一直在绞尽脑汁,想弄明白为什么这样做行不通。我在核心数据中有一对多的关系,并且正在尝试将值正确地传递给细节序列 下面是数据模型(属性为二进制数据类型): 如果我在configureCell:方法中获取数据,它将按预期提供值:Ios 一对多关系核心数据:在prepareForSegue中获取:,ios,uitableview,core-data,Ios,Uitableview,Core Data,在过去的几个小时里,我一直在绞尽脑汁,想弄明白为什么这样做行不通。我在核心数据中有一对多的关系,并且正在尝试将值正确地传递给细节序列 下面是数据模型(属性为二进制数据类型): 如果我在configureCell:方法中获取数据,它将按预期提供值: - (void)configureCell:(UITableViewCell *)cell atIndexPath(NSIndexPath *)indexPath { TBAppDelegate *delegate = (TBAppDelegat
- (void)configureCell:(UITableViewCell *)cell atIndexPath(NSIndexPath *)indexPath
{
TBAppDelegate *delegate = (TBAppDelegate *)[[UIApplication sharedApplication] delegate];
managedObjectContext = [delegate managedObjectContext];
self.everything = [fetchedResultsController.fetchedObjects objectAtIndex:indexPath.section];
NSArray *arg = [NSKeyedUnarchiver unarchiveObjectWithData:self.everything.urls.urls];
NSLog (@"%@, [arg valueForKey:@"source"]); // this prints out a list of stored urls containing locations of jpeg images
}
我的NSFetchedResultsController:
- (NSFetchedResultsController *)fetchedResultsController
{
TBAppDelegate *delegate = (TBAppDelegate *)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = delegate.managedObjectContext;
NSFetchRequest *fetchRequest = nil;
fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = nil;
entity = [NSEntityDescription entityForName:@"Everything" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:INFINITY];
NSSortDescriptor *albumDescriptor = nil;
albumDescriptor = [[NSSortDescriptor alloc] initWithKey:@"album_names" ascending:NO];
NSSortDescriptor *urlDescriptor = nil;
urlDescriptor = [[NSSortDescriptor alloc] initWithKey:@"urls.urls" ascending:NO];
NSArray *sortDescriptors = nil;
sortDescriptors = [[NSArray alloc] initWithObjects:albumDescriptor, urlDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *frc = nil;
frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
[frc setDelegate:self];
[self setFetchedResultsController:frc];
return frc;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// This is where the actual crash happens. It complains about [_NSDictionaryM urls:] unrecognized selector sent to instance
NSArray *count = [NSKeyedUnarchiver unarchiveObjectWithData:self.everythingGroup.urls.urls];
return [count count];
}
这是我的备考方式。这部分不起作用。当我尝试设置局部视图控制器的截面计数时,它会因无法识别的选择器错误而崩溃
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath;
indexPath = [self.tableView indexPathForSelectedRow];
TBAppDelegate *delegate = (TBAppDelegate *)[[UIApplication sharedApplication] delegate];
managedObjectContext = [delegate managedObjectContext];
self.everything = [fetchedResultsController.fetchedObjects objectAtIndex:indexPath.section];
NSArray *data = [NSKeyedUnarchiver unarchiveObjectWithData:self.everything.album_names];
NSArray *urls = [NSKeyedUnarchiver unarchiveObjectWithData:self.everything.urls.urls];
TBEverythingDetailViewController *details = segue.destinationViewController;
details.everything = [[data objectAtIndex:indexPath.section]valueForKey:@"name"];
self.urls.urls = [fetchedResultsController.fetchedObjects objectAtIndex:indexPath.section];
details.everythingGroup = [urls objectAtIndex:indexPath.section];
}
局部视图控制器的一部分:
- (NSFetchedResultsController *)fetchedResultsController
{
TBAppDelegate *delegate = (TBAppDelegate *)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = delegate.managedObjectContext;
NSFetchRequest *fetchRequest = nil;
fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = nil;
entity = [NSEntityDescription entityForName:@"Everything" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:INFINITY];
NSSortDescriptor *albumDescriptor = nil;
albumDescriptor = [[NSSortDescriptor alloc] initWithKey:@"album_names" ascending:NO];
NSSortDescriptor *urlDescriptor = nil;
urlDescriptor = [[NSSortDescriptor alloc] initWithKey:@"urls.urls" ascending:NO];
NSArray *sortDescriptors = nil;
sortDescriptors = [[NSArray alloc] initWithObjects:albumDescriptor, urlDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *frc = nil;
frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
[frc setDelegate:self];
[self setFetchedResultsController:frc];
return frc;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// This is where the actual crash happens. It complains about [_NSDictionaryM urls:] unrecognized selector sent to instance
NSArray *count = [NSKeyedUnarchiver unarchiveObjectWithData:self.everythingGroup.urls.urls];
return [count count];
}
我觉得我在这里遗漏了一些明显的步骤来解释为什么它不起作用。我能帮忙吗 Your self.everythinggrpup是一个NSDictionary,因此不响应选择器“URL”。当你将NSDictionary而不是Everything对象分配给self.everythinggroup时。因此,您不需要这样做,或者从字典中取出everything对象,然后请求它提供URL。取决于您是如何设置的。您确实将详细信息everythingGroup设置为:
NSArray *urls = [NSKeyedUnarchiver unarchiveObjectWithData:self.everything.urls.urls];
details.everythingGroup = [urls objectAtIndex:indexPath.section];
正如我在URL.URL中看到的,URL是归档到NSData中的字典数组。所以详细信息。everythingGroup
现在是一本字典,但您访问它的方式是:
NSArray*count=[NSKeyedUnarchiver unarchiveObjectWithData:self.everythingGroup.url.url]代码>
它在这里坠毁是正常的。
当您访问它时,它看起来像是一个详细信息。everythingGroup
应该是一个Everything
您实际上是从-fetchedResultsController返回一个NSFetchedResultsController吗?