2010-12-21 184 views
1

我設計了這樣一個數據結構:iPhone - 核心數據中獲取數據

employee 
. department 
. building 
. sector 
> number (this is a relationship to employeeData) 



employeeData 
. name 
. position 
. salary 
> number (this is a relationship to employee) 

這樣一來,將有每名僱員只有一個EmployeeData工作項。

我有所有實體的類。

現在,我該如何獲取匹配特定號碼的每個員工,然後獲取與員工相對應的employeeData?

我需要的是這個

「找到EmployeeData工作的employee.number = X」

感謝

回答

3

事情是這樣的:

NSManagedObjectContext * context = [[NSApp delegate] managedObjectContext]; 
NSManagedObjectModel * model = [[NSApp delegate] managedObjectModel]; 
NSDictionary   * entities = [model entitiesByName]; 
NSEntityDescription * entity = [entities valueForKey:@"Employee"]; 

NSPredicate * predicate; 
predicate = [NSPredicate predicateWithFormat:@"number = %@", number]; 


NSFetchRequest * fetch = [[NSFetchRequest alloc] init]; 
[fetch setEntity: entity]; 
[fetch setPredicate: predicate]; 


NSArray * results = [context executeFetchRequest:fetch error:nil]; 
[fetch release]; 

Employee *emp = [results objectAtIndex:0]; 
EmployeeData *data = [emp data]; 

請記住,數據關係是自動如果關係是在覈心數據模型中定義的,則提取到期。這是提取和修改,以適應以下問題:http://www.cocoadevcentral.com/articles/000086.php

+0

謝謝!!!!!!!!!!!最後一行有一個小問題,因爲數據沒有被聲明,但基於此,我得到了正確的答案! – SpaceDog 2010-12-21 21:40:36