NSPredicate 谓词

132 篇文章 0 订阅

There are times during development when you need to match text against a search phrase. NSString offers a few simple built-in routines that offer basic comparison options. For example, use isEqualToString: to compare two strings for equality. Often, you need more flexible searching. NSString can take you along that path, but only so far. NSString lets you do the following:

  • You can test if a string hasPrefix: or hasSuffix: to find matches at the start or end of your string.
  • When the range returned by rangeOfString: has its location set to NSNotFound, you know the two strings do not match.
  • NSString also offers a number of comparison functions like compare: and caseInsensitiveCompare:, which will show you whether the strings are in ascending or descending order.

For more advanced string matching consider using NSPredicates. This class, which supports a huge range of comparison options, offers both substring and regular expression matching. Here are a couple of examples that match a string to a phrase using NSPredicates:

  1. BOOL checkmatch(NSString *item, NSString *matchphrase)
  2. {
  3. BOOL match = NO;
  4. NSPredicate *containPred = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", matchphrase];
  5. match = match | [containPred evaluateWithObject:item];
  6. NSPredicate *matchPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", matchphrase];
  7. match = match | [matchPred evaluateWithObject:item];
  8. return match;
  9. }

The first example matches based on a set substring. If the matchphrase appears in the string, the predicate evaluates to true. The two parameters in the square brackets modify the "contains" request. The c means match in a case insensitive manner; the d refers to a diacritic insensitive lookup.

The second example performs a regular expression match. This match is evaluated against the entire string. If you search for ".a", you would match "Ba" but not "Bologna." To match both, search for ".*a" instead.

Predicates can also help match against strings in arrays. Recently, I needed to recover arguments for a command-line utility. Filtering my predicate helped me recover only those arguments that were flags. Here's the code I used to search for items that started with "-".

  1. NSArray *args = [[NSProcessInfo processInfo] arguments];
  2. NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF beginswith '-'"];
  3. NSArray *dashedArgs = [args filteredArrayUsingPredicate:pred];

This search used a style of predicate that searched for strings that began with a matching item. When applied against my array, it filtered out just those items that matched my request, producing my list of flags.

Apple offers an in-depth guide to creating and using predicates. This construct can go far beyond simple string comparisons, allowing you to compare complex structures and objects, and it's a great tool to have on-hand even when you are just working with strings. Predicates simplify string comparison functions, allowing you to build general match tests with very few lines of readable and easy-to-maintain code.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值