如何使用predicateWithFormat查询NSSet中包含的NSString [英] How can I use predicateWithFormat for query a NSString is contained in an NSSet
本文介绍了如何使用predicateWithFormat查询NSSet中包含的NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用StackMob作为后端。我有一个 Event
实体,该实体的关系称为用户 users
(类型为 NSSet
)。现在,我想获取某些用户的用户名
为 @ someuser
的所有事件。我的代码是这样的:
Using StackMob as backend. I have an Event
entity which has a relationship called user users
(type NSSet
). Right now I want to get all the events that some user's username
is @"someuser"
. My code like this:
[self.client getLoggedInUserOnSuccess:^(NSDictionary *result) {
NSString *currentlyLoggedInUser = [result valueForKey:@"username"];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *eventEntity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedOjbectContext];
[request setEntity:eventEntity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY users.username = %@", currentlyLoggedInUser];
NSLog(@"predicate: %@", predicate);
[request setPredicate:predicate];
NSLog(@"request: %@", request);
events = [NSArray new];
events = [self.managedOjbectContext executeFetchRequest:request error:nil];
/* this is working...
for (Event *e in events) {
NSLog(@"%@",[e.users filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"username == %@", currentlyLoggedInUser]]);
}
*/
NSLog(@"events: %@", events);
} onFailure:^(NSError *error) {
}];
推荐答案
如果我正确理解了您的问题,则必须使用以下谓词:
If I understand your question correctly, you have to use the following predicate:
NSString *userName = @"some user";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY users.username = %@", userName];
它将查找具有任何具有给定用户名的用户的所有事件。
It finds all events that have any user with the given user name.
这篇关于如何使用predicateWithFormat查询NSSet中包含的NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文