从不在集合中的核心数据中获取对象

发布于 2024-11-10 17:09:51 字数 988 浏览 5 评论 0原文

我试图从不在给定集合中的核心数据中获取对象,但我无法让它工作。

例如,假设我们有一个名为 User 的核心数据实体,它具有一些属性,例如 userName、familyName、givenName 和 active。给定一个表示一组用户名的字符串数组,我们可以轻松获取与该用户名列表相对应的所有用户:

NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User"
                                          inManagedObjectContext:moc];
[request setEntity:entity];

NSArray *userNames = [NSArray arrayWithObjects:@"user1", @"user2", @"user3", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userName IN %@", userNames];
[request setPredicate:predicate];
NSArray *users = [moc executeFetchRequest:request error:nil];

但是,我想获取该组的补集,即,我想要核心数据中不存在的所有用户t 在 userNames 数组中指定用户名。有谁知道如何解决这个问题?我认为在谓词 中添加 "NOT" 就足够简单了(即“userName NOT IN %@”),但是 Xcode 抛出一个异常,指出该谓词无法解析格式。我还尝试使用可用于获取请求的谓词构建器,但没有成功。该文档也不是特别有帮助。建议?评论?感谢您的帮助:)

I'm trying to fetch objects from core data that are not in a given set, but I haven't been able to get it to work.

For instance, suppose that we have a core data entity named User, which has a few attributes such as userName, familyName, givenName, and active. Given an array of strings representing a set of usernames, we can easily fetch all the users corresponding to that list of usernames:

NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User"
                                          inManagedObjectContext:moc];
[request setEntity:entity];

NSArray *userNames = [NSArray arrayWithObjects:@"user1", @"user2", @"user3", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userName IN %@", userNames];
[request setPredicate:predicate];
NSArray *users = [moc executeFetchRequest:request error:nil];

However, I want to fetch the complement of that set, i.e., I want all the users in core data that don't have the usernames specified in the userNames array. Does anyone have an idea how to approach this issue? I thought it would be simple enough to add a "NOT" in the predicate (i.e., "userName NOT IN %@"), but Xcode throws an exception saying the predicate format could not be parsed. I also tried using the predicate builder available for fetch requests with no luck. The documentation wasn't particularly helpful either. Suggestions? Comments? Thanks for all your help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-11-17 17:09:51

为了找到不在数组中的对象,您所要做的就是这样:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (userName IN %@)", userNames];

这应该返回所有对象的请求,而不包含您指定的对象

In order to find the objects that aren't in your array, all you have to do is something like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (userName IN %@)", userNames];

That should return a request of all the objects without the ones you specified

爱你不解释 2024-11-17 17:09:51

我不擅长核心数据/objective-c,但谓词应该像下面的语句;

[predicateFormat appendFormat:@"not (some_field_name in {'A','B','B','C'})"];

一个例子:

NSMutableString * mutableStr = [[NSMutableString alloc] init];

//prepare filter statement
for (SomeEntity * e in self.someArray) {
    [mutableStr appendFormat:@"'%@',", e.key];
}

//excluded objects exist
if (![mutableStr isEqual:@""])
{
    //remove last comma from mutable string
    mutableStr = [[mutableStr substringToIndex:mutableStr.length-1] copy];

    [predicateFormat appendFormat:@"not (key in {%@})", mutableStr];
}

//...
//use this predicate in NSFetchRequest
//fetchRequest.predicate = [NSPredicate predicateWithFormat:predicateFormat];
//...   

I am not strong at core data/objective-c but the predicate should be like the following statement;

[predicateFormat appendFormat:@"not (some_field_name in {'A','B','B','C'})"];

An example:

NSMutableString * mutableStr = [[NSMutableString alloc] init];

//prepare filter statement
for (SomeEntity * e in self.someArray) {
    [mutableStr appendFormat:@"'%@',", e.key];
}

//excluded objects exist
if (![mutableStr isEqual:@""])
{
    //remove last comma from mutable string
    mutableStr = [[mutableStr substringToIndex:mutableStr.length-1] copy];

    [predicateFormat appendFormat:@"not (key in {%@})", mutableStr];
}

//...
//use this predicate in NSFetchRequest
//fetchRequest.predicate = [NSPredicate predicateWithFormat:predicateFormat];
//...   
℡Ms空城旧梦 2024-11-17 17:09:51

这是另一个有用的示例,展示了如何获取字符串列表,并过滤掉任何不以字母 AZ 开头的字符串:

NSArray* listOfCompanies = [NSArray arrayWithObjects:@"123 Hello", @"-30'c in Norway", @"ABC Ltd", @"British Rail", @"Daily Mail" @"Zylophones Inc.", nil];

NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF MATCHES[c] '^[A-Za-z].*')"];

NSArray *filteredList = [listOfCompanies filteredArrayUsingPredicate:bPredicate];

for (NSString* oneCompany in filteredList)
    NSLog(@"%@", oneCompany);

当我填充 时,我使用这种 NSPredicate UITableView 带有 AZ 索引,并且希望为不以字母开头的项目提供“所有其他”部分。

Here's another useful example, showing how to take a list of strings, and filter out any which DON'T start with the letters A-Z:

NSArray* listOfCompanies = [NSArray arrayWithObjects:@"123 Hello", @"-30'c in Norway", @"ABC Ltd", @"British Rail", @"Daily Mail" @"Zylophones Inc.", nil];

NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF MATCHES[c] '^[A-Za-z].*')"];

NSArray *filteredList = [listOfCompanies filteredArrayUsingPredicate:bPredicate];

for (NSString* oneCompany in filteredList)
    NSLog(@"%@", oneCompany);

I use this kind of NSPredicate when I'm populating a UITableView with an A-Z index, and want an "everything else" section for items which don't start with a letter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文