在我的應用程序中,有多個畫廊。所以我創建了一個名爲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;
}
我怎麼能只檢索對象例如,對於藝術,然後顯示其描述?
self.galleries的數據類型是什麼?像數組或其他東西,那麼我可以解決你的問題 –
它的一個'NSArray' – Isuru