且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NSPredicate查询是否不包含特定字符串

更新时间:2023-02-26 10:58:59

您的第一个谓词

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