1

在我的应用程序中,有多个画廊。所以我创建了一个名为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,然后显示其描述?

4

4 回答 4

2
NSArray *galleryArray = [self.managedObjectContext executeFetchRequest:fetchRequst error:&error];


    for (Gallery *gallery in galleryArray)
    {
        if(gallery.galleryName isEqualToString:@"Art"){
            //this is gallery Object is of Art
         } else if (gallery.galleryName isEqualToString:@"Architecture"){
             //this is gallery Object is of Architecture
         }
    }
于 2013-08-22T08:04:21.107 回答
2

具体来说,我们也可以使用谓词方法并通过传递画廊名称来调用该函数(例如@“Art”,@“Architecture”)

+ (Gallery *)getGalleryFor:(NSString *)galleryName
 {
      NSFetchRequest *fetchRequst = [[NSFetchRequest alloc] init];
      NSEntityDescription *entity = [NSEntityDescription entityForName:@"Gallery" inManagedObjectContext:self.managedObjectContext];
      [fetchRequest setEntity:entity];
      NSPredicate *pred = [NSPredicate predicateWithFormat:@"galleryName == %@",galleryName];
      [request setPredicate:pred];
      NSError *error;
      NSArray *array = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

     return [array lastObject];
 }
于 2013-08-22T09:36:41.700 回答
1

您需要快速枚举获取的请求:

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

for(Gallery *g in self.galleries){
    if([g.galleryName isEqualToString:@"Art"]){
        self.galleryInfoTextView.text = g.galleryDesc;
    }
    //blah blah blah
}
于 2013-08-22T08:04:03.363 回答
0

如果self.gallery从前一个(可能是从 a 上的选择UITableView)传递到视图控制器,那么你为什么不能这样做:

self.galleryInfoTextView.text = self.gallery.galleryDesc;
于 2013-08-22T08:01:31.783 回答