2013-08-22 90 views
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; 
} 

我怎麼能只檢索對象例如,對於藝術,然後顯示其描述?

+0

self.galleries的數據類型是什麼?像數組或其他東西,那麼我可以解決你的問題 –

+0

它的一個'NSArray' – Isuru

回答

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 
     } 
    } 
+0

這肯定會爲你工作只是嘗試這一次 –

0

如果self.gallery被傳遞到視圖控制器與以前的(大概是從上一個UITableView選擇),那麼爲什麼你就不能做到這一點:

self.galleryInfoTextView.text = self.gallery.galleryDesc; 
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 
} 
2

具體而言,我們還可以使用謂詞方法,並通過將畫廊的名字調用這個函數(如@「藝術」,@「建築」)

+ (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]; 
}