When we Fetch Request, why do we use Like instead of ==?
Like
NSPredicate *predicate开发者_Python百科 = [NSPredicate predicateWithFormat:@"%K like %@",
attributeName, attributeValue];
Why not
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@",
attributeName, attributeValue];
Honestly, you're just following the pattern that the creators made. They could have implemented it using ==
, but it would have made less sense. Generally, ==
means exactly equal to, as in references being equal. This is a pattern match, so you want to find items that are "like" a pattern, not necessarily equal to a pattern.
NSPredicate
's support using the ==
operator, but it performs differently than the like
operator does:
NSPredicate *newPredicate = [NSPredicate predicateWithFormat:@"anAttribute == %@", [NSNumber numberWithBool:aBool]];
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"anAttribute == YES"];
精彩评论