2012-12-13 65 views
0

從數據庫文件中取數據我有兩個NSManagedObject類的UserInfo和部門(一對一和一對多的關係分別),這裏是我的代碼,問題使用NSPredicate

// UserInfo.h

@interface UserInfo : NSManagedObject 

@property (nonatomic, retain) NSDate * bDate; 
@property (nonatomic, retain) NSString * firstName; 
@property (nonatomic, retain) NSString * lastName; 
@property (nonatomic, retain) NSString * userName; 
@property (nonatomic, retain) Department *dept; 

@end 

//Department.h

@class UserInfo; 

@interface Department : NSManagedObject 

@property (nonatomic, retain) NSString * id; 
@property (nonatomic, retain) NSString * post; 
@property (nonatomic, retain) NSString * location; 
@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) NSSet *user; 
@end 

@interface Department (CoreDataGeneratedAccessors) 

- (void)addUserObject:(UserInfo *)value; 
- (void)removeUserObject:(UserInfo *)value; 
- (void)addUser:(NSSet *)values; 
- (void)removeUser:(NSSet *)values; 
@end 

從DB I使用,其效果很好

搜索特定用戶
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstName contains[cd] %@", NameTosearch]; 

但是,現在我可以用什麼樣的謂詞來獲取信息連接到特定部門的所有用戶?

回答

1

您還可以使用與謂詞中的對象進行比較。

I.e.如果你在部門的指針往一處實例:

... withFormat代碼:@「部門=%@」,部門」

內部CoreData將與對象的管理對象標識符代替%@ 。