ios 不包含特定字符串的 NSPredicate 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12534629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:18:38  来源:igfitidea点击:

NSPredicate query for not containing a specific string

ioscore-datanspredicate

提问by sangony

Looked high and low for this one but can't find my answer. I am looking to query core data for all records which are NOT equal to a specified string. For example, all records which are not equal to the current session ID. I have tried these to no avail:

看了这个高低,但找不到我的答案。我希望查询所有不等于指定字符串的记录的核心数据。例如,所有不等于当前会话 ID 的记录。我试过这些都无济于事:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID != %@", [sitListingID objectAtIndex:i]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ NOT CONTAINS[cd] %@",@"listingID", [sitListingID objectAtIndex:i]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ NOT CONTAINS %@",@"listingID", [sitListingID objectAtIndex:i]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID NOT like %@", [sitListingID objectAtIndex:i]];

Nothing works.HEEEEELP!!! ----------------------------------------------------- more code

什么都不起作用。嘿嘿!!!-------------------------------------------------- --- 更多代码

NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ListingRecord" inManagedObjectContext:context];
    [request setEntity:entity];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sessionID <> %@", uniqueSessionListings];
    [request setPredicate:predicate];
    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];

回答by Martin R

Your first predicate

你的第一个谓词

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID != %@", sessionID];

should work to find all records where the attribute listingIDis not equal to sessionID(provided that listingIDand sessionIDhave the same type).

应该可以找到属性listingID不等于的所有记录sessionID(前提是listingID并且sessionID具有相同的类型)。

If both are strings and you want to find all records where listingIDdoes not containthe string sessionIDas a substring, then this predicate should work:

如果两者都是字符串,并且您想查找listingID不包含字符串sessionID作为子字符串的所有记录,则此谓词应该有效:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (listingID CONTAINS %@)", sessionID];

Use "CONTAINS[cd]" if the string comparison should be done case and diacritical insensitive.

使用“contains [CD]”如果字符串比较应该做的事情况和变音不区分大小写。

NOTE: You can specify the attribute name as an argument, but then you must use %Kinstead of %@as format:

注意:您可以将属性名称指定为参数,但您必须使用%K代替%@格式:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (%K CONTAINS %@)", @"listingID", sessionID];