使用NSPredicate从db文件获取数据的问题

时间:2012-12-13 06:00:11

标签: iphone nspredicate

我有两个NSManagedObject类,UserInfo和Department(分别为to-one和to-many关系)这里是我的代码,

// 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];

但是,现在我可以用什么样的谓词来获取与特定部门相关的信息的所有用户?

2 个答案:

答案 0 :(得分:1)

您还可以在谓词中使用与对象的比较。

即。如果你有一个指向部门中的Department实例的指针:

... withFormat:@“dept =%@”,dept“

内部CoreData将%@替换为对象的托管对象标识符。

答案 1 :(得分:0)

使用

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

关注此博客以获取更多信息http://rajneesh071.blogspot.in/
并按照我的回答StackOverflow