LoginSignup
13
12

More than 5 years have passed since last update.

NSPredicateで日本語の濁音、半濁音を正しく比較する

Last updated at Posted at 2015-03-05

NSPredicateで、単純に日本語の濁音、半濁音を比較しようと思うと、正しくマッチしないケースがあります。
これの解決法を書きます。

解決方法

結論から言うと解決するには、比較指定子にdを付けるだけです。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[d] %@", query];
NSString *filteredArray = [array filteredArrayUsingPredicate:predicate];

NSPredicateで正しく濁音、半濁音が扱えない理由

NSPredicateでの比較はデフォルトではdiacritic sensitiveであり、NSStringの検索系メソッドにNSLiteralSearchを渡した時と同じ挙動をするためです。そのためデフォルトでは、正規化方法が異なる濁音、半濁音を正しく比較できません。

String comparisons are, by default, case and diacritic sensitive
Predicate Programming Guideより引用。

まとめ

NSPredicateで日本語の比較をする際にはdオプションをつけましょう。

13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12