A definition of logical conditions used to constrain a search either for a fetch or for in-memory filtering.
Predicates represent logical conditions, which you can use to filter collections of objects. Although it’s common to create predicates directly from instances of NSComparisonPredicate, NSCompoundPredicate, and NSExpression, you often create predicates from a format string which is parsed by the class methods on NSPredicate.
//检查一个Object对象是否可以通过验证 - (BOOL)evaluateWithObject:(nullableid)object; // evaluate a predicate against a single object
//用谓词模板进行对象的验证 - (BOOL)evaluateWithObject:(nullableid)object substitutionVariables:(nullableNSDictionary<NSString *, id> *)bindings API_AVAILABLE(macos(10.5), ios(3.0), watchos(2.0), tvos(9.0)); // single pass evaluation substituting variables from the bindings dictionary for any variable expressions encountered
//允许安全评估 - (void)allowEvaluation API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0)); // Force a predicate which was securely decoded to allow evaluation
@interface NSArray<ObjectType> (NSPredicateSupport) //根据对象数组计算谓词并返回已过滤的数组 - (NSArray<ObjectType> *)filteredArrayUsingPredicate:(NSPredicate *)predicate; // evaluate a predicate against an array of objects and return a filtered array @end
@interface NSMutableArray<ObjectType> (NSPredicateSupport) //根据对象数组计算谓词并直接过滤可变数组 - (void)filterUsingPredicate:(NSPredicate *)predicate; // evaluate a predicate against an array of objects and filter the mutable array directly @end
@interface NSSet<ObjectType> (NSPredicateSupport) //根据一组对象评估谓词并返回一个过滤集 - (NSSet<ObjectType> *)filteredSetUsingPredicate:(NSPredicate *)predicate API_AVAILABLE(macos(10.5), ios(3.0), watchos(2.0), tvos(9.0)); // evaluate a predicate against a set of objects and return a filtered set @end
@interface NSMutableSet<ObjectType> (NSPredicateSupport) //根据一组对象评估谓词并直接过滤可变集 - (void)filterUsingPredicate:(NSPredicate *)predicate API_AVAILABLE(macos(10.5), ios(3.0), watchos(2.0), tvos(9.0)); // evaluate a predicate against a set of objects and filter the mutable set directly @end
@interface NSOrderedSet<ObjectType> (NSPredicateSupport) //根据有序对象集评估谓词并返回已过滤的有序集 - (NSOrderedSet<ObjectType> *)filteredOrderedSetUsingPredicate:(NSPredicate *)p API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)); // evaluate a predicate against an ordered set of objects and return a filtered ordered set
@end
@interface NSMutableOrderedSet<ObjectType> (NSPredicateSupport) //根据有序对象集评估谓词,并直接过滤可变有序集 - (void)filterUsingPredicate:(NSPredicate *)p API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)); // evaluate a predicate against an ordered set of objects and filter the mutable ordered set directly
// Describes how the operator is modified: can be direct, ALL, or ANY typedef NS_ENUM(NSUInteger, NSComparisonPredicateModifier) { NSDirectPredicateModifier = 0, // Do a direct comparison//直接进行比较操作 NSAllPredicateModifier, // ALL toMany.x = y//只有当内部所有元素都通过验证时,才算通过 NSAnyPredicateModifier // ANY toMany.x = y//当内部有一个元素满足时,就算通过验证 //后两个一般用于数组 };
// Type basic set of operators defined. Most are obvious; NSCustomSelectorPredicateOperatorType allows a developer to create an operator which uses the custom selector specified in the constructor to do the evaluation. typedef NS_ENUM(NSUInteger, NSPredicateOperatorType) { NSLessThanPredicateOperatorType = 0, // compare: returns NSOrderedAscending// 小于 NSLessThanOrEqualToPredicateOperatorType, // compare: returns NSOrderedAscending || NSOrderedSame// 小于等于 NSGreaterThanPredicateOperatorType, // compare: returns NSOrderedDescending// 大于 NSGreaterThanOrEqualToPredicateOperatorType, // compare: returns NSOrderedDescending || NSOrderedSame// 大于等于 NSEqualToPredicateOperatorType, // isEqual: returns true// 等于 NSNotEqualToPredicateOperatorType, // isEqual: returns false//不等于 NSMatchesPredicateOperatorType,// 正则比配 NSLikePredicateOperatorType,// Like匹配 NSBeginsWithPredicateOperatorType,// 左边的表达式以右边的表达式作为开头 NSEndsWithPredicateOperatorType,// 左边的表达式以右边的表达式作为结尾 NSInPredicateOperatorType, // rhs contains lhs returns true// 左边的表达式出现在右边的集合中 NSCustomSelectorPredicateOperatorType,// 使用自定义的函数来进行验证 NSContainsPredicateOperatorType API_AVAILABLE(macos(10.5), ios(3.0), watchos(2.0), tvos(9.0)) = 99, // lhs contains rhs returns true// 左边的集合包括右边的元素 NSBetweenPredicateOperatorType API_AVAILABLE(macos(10.5), ios(3.0), watchos(2.0), tvos(9.0))// 左边表达式的值在右边的范围中 };
// Flags(s) that can be passed to the factory to indicate that a operator operating on strings should do so in a case insensitive fashion. typedef NS_OPTIONS(NSUInteger, NSComparisonPredicateOptions) { NSCaseInsensitivePredicateOption = 0x01,// 不区分大小写 NSDiacriticInsensitivePredicateOption = 0x02,// 不区分读音符号 NSNormalizedPredicateOption API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = 0x04, /* Indicate that the strings to be compared have been preprocessed; this supersedes other options and is intended as a performance optimization option *///比较前进行预处理 代替上面两个选项 };