2012-03-28 84 views
0

我需要將使用來自核心數據圖表的提取請求檢索到的值放入數組中,但不完全確定如何處理此問題。將核心數據值放入數組

我用下面執行讀取:

NSString *entityName = @"Project"; // Put your entity name here 
     NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName); 

     // 2 - Request that Entity 
     NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName]; 

     // 3 - Filter it if you want 
     request.predicate = [NSPredicate predicateWithFormat:@"belongsToProject = %@", _selectedProject]; 

     // 4 - Sort it if you want 
     request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"dateTaken" 
                         ascending:YES 
                          selector:@selector(localizedCaseInsensitiveCompare:)]]; 
     // 5 - Fetch it 
     self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request 
                      managedObjectContext:self.managedObjectContext 
                       sectionNameKeyPath:nil 
                         cacheName:nil]; 


[self performFetch]; 

正如你所看到的,我是過濾回來使用NSPredicate的值。

如何將這些值獲取到數組中,並且還可以在實體處於數組中時爲其選擇單個屬性,例如project.description或project.name?

感謝Eimantas我有一個數組的對象,但是我仍然需要做兩件事情:通過陣列和輸出數據

  1. 循環到一些HTML
  2. 單獨選擇從屬性該數組,例如項目描述。

我使用以下for循環,要做的第一件:

for (int i=0; i < [projectListArray count]; i++) 
{ 
     NSString *tmp = (NSString *)[projectListArray objectAtIndex:i]; 
} 

然而,這是返回錯誤:

-[Project length]: unrecognized selector sent to instance 0x1b9f20 
2012-03-28 10:48:35.160 Project App[3973:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Project length]: unrecognized selector sent to instance 0x1b9f20' 

看來好像我可能不會增加?

回答

1

[self.fetchedResultsController fetchedObjects]返回獲取對象的數組。

更新

這是更好地使用快速enumaration,不是for循環:

for (Project *project in projectListArray) { 
    NSString *projectDescription = [project valueForKey:@"description"]; 
} 

你得到,因爲你投對象NSString異常,而這是一個指針(我想)Project管理對象。

+0

輝煌,謝謝你Eimantas。如果你不介意幫助,我有一件我想做的事情呢?我已經更新了我原來的問題。 – jcrowson 2012-03-28 09:51:36