NSPredicate查询是否不包含特定字符串 [英] NSPredicate query for not containing a specific string
问题描述
为此寻找高低,却找不到我的答案.我正在寻找查询不等于指定字符串的所有记录的核心数据.例如,所有不等于当前会话ID的记录.我尝试了这些都无济于事:
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:
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]];
没有任何效果.EEEEEELP!!!-----------------------------------------------------更多代码
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];
推荐答案
您的第一个谓词
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID != %@", sessionID];
应该查找所有属性为 listingID
不等于 sessionID
的记录(前提是 listingID
和 sessionID
具有相同的类型.
should work to find all records where the attribute listingID
is not equal to sessionID
(provided that listingID
and sessionID
have the same type).
如果两个都是字符串,并且您想查找所有 listingID
不包含字符串 sessionID
作为子字符串的记录,则该谓词应该工作:
If both are strings and you want to find all records where listingID
does not contain the string sessionID
as a substring, then this predicate should work:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (listingID CONTAINS %@)", sessionID];
如果应该比较大小写且不区分变音符,请使用"CONTAINS [cd]".
Use "CONTAINS[cd]" if the string comparison should be done case and diacritical insensitive.
注意:可以将属性名称指定为参数,但是必须使用%K
而不是%@
作为格式:
NOTE: You can specify the attribute name as an argument, but then you must use %K
instead of %@
as format:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (%K CONTAINS %@)", @"listingID", sessionID];
这篇关于NSPredicate查询是否不包含特定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!