我有一个小核心数据库"来宾"我试图从一个名为FetchRequestA的获取请求模板中获取结果,我创建了一个按钮,在控制台中触发请求的结果,但我一直得到一个空答案,请求设置为显示包含广告的所有客户名称?这是我正在使用的代码:
- (IBAction)fetchA:(id)sender {
NSFetchRequest *request2 = [[[self managedObjectModel] fetchRequestTemplateForName:@"FetchRequestA"] copy];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]initWithKey:@"guestlastname" ascending:YES];
[request2 setSortDescriptors:[NSArray arrayWithObject:sort]];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sort, nil];
[request2 setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:request2 error:&error];
if (fetchedObjects == nil) {
NSLog(@"problem %@", error);
}
for (Guests *guestlastname in fetchedObjects) {
NSLog(@"Fetched Object = %@", guestlastname.guestlastname);
}
}
我错过了一个方法吗?我已经仔细考虑过但无济于事,提前谢谢。
答案 0 :(得分:0)
这是解决方案:
(IBAction)gettemplatebutton:(id)sender {
AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = [appDelegate managedObjectContext];
NSManagedObjectModel * model = [[context persistentStoreCoordinator] managedObjectModel];
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:self.fetchedObjects,@“guestlastname”,nil];
NSFetchRequest * request2 = [model fetchRequestFromTemplateWithName:@“FetchRequestA”substitutionVariables:dict];
NSError * error = nil;
NSArray * Guests2 = [context executeFetchRequest:request2 error:& error];
NSString * g3 = @“”;
for(来宾2中的NSManagedObject * guestlastname)
{ g3 = [g3 stringByAppendingString:[NSString stringWithFormat:@“%@ \ n”,[guestlastname valueForKey:@“guestlastname”]]]; }
self.displaytemplateLabel.text = g3;
[_ displaytemplateLabel setNumberOfLines:0];
for(来宾2中的NSManagedObject * guestlastname)
{ { NSLog(@“%@”,[guestlastname valueForKey:@“guestlastname”]); } } }
并添加了@property(非原子,保留)NSArray * fetchedObjects;在头文件中。