开发者

When we Fetch Request, why do we use Like instead of ==?

开发者 https://www.devze.com 2023-03-05 06:40 出处:网络
Like NSPredicate *predicate开发者_Python百科 = [NSPredicate predicateWithFormat:@\"%K like %@\",

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

精彩评论

暂无评论...
验证码 换一张
取 消