在我的应用程序中,有多个画廊。所以我创建了一个名为Gallery
并插入了一些对象的实体。像这样,
NSManagedObjectContext *context = [self managedObjectContext];
Gallery *gallery1 = [NSEntityDescription insertNewObjectForEntityForName:@"Gallery" inManagedObjectContext:context];
gallery1.galleryId = @1;
gallery1.galleryName = @"Art";
gallery1.galleryDesc = @"Some info about art";
Gallery *gallery2 = [NSEntityDescription insertNewObjectForEntityForName:@"Gallery" inManagedObjectContext:context];
gallery2.galleryId = @2;
gallery2.galleryName = @"Architecture";
gallery2.galleryDesc = @"details about architecture";
NSError *error;
if (![context save:&error]) {
NSLog(@"Error Ocurred When Saving: %@", error.localizedDescription);
}
现在在视图控制器中,我需要根据所选画廊检索有关特定画廊的描述。
下面是我到目前为止的代码
NSFetchRequest *fetchRequst = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Gallery" inManagedObjectContext:self.managedObjectContext];
fetchRequst.entity = entity;
NSError *error;
// Gets the Gallery objects to an array
self.galleries = [self.managedObjectContext executeFetchRequest:fetchRequst error:&error];
// self.gallery is passed in to this view controller from the previous one
if ([self.gallery isEqualToString:@"Art"]) {
self.galleryInfoTextView.text = gallery.galleryDesc;
} else if ([self.gallery isEqualToString:@"Architecture"]) {
self.galleryInfoTextView.text = gallery.galleryDesc;
}
如何仅检索对象,例如 Art,然后显示其描述?