10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[iOS] 同じようにみえる文字が、[NSString isEqualToString:] で false になってしまう

Last updated at Posted at 2014-10-24

編集履歴

  • 20170410 原因

#問題
一見同じようにみえる2つの文字列(例:プ)が一致しない。

Example
NSString*		str1		= @"プ";
NSString*		str2		= @"プ";
			
if([str1 isEqualToString:str2]) {
	NSLog(@"OK");
}else {
	NSLog(@"NG");
}

出力 => NG

#原因

  • Unicode正規化形式である NFC と NFD による文字列なため
    • 例えば、日本語ファイル名のファイルを iOS 上においた際、ファイルシステム上では NFD。

#解決方法
NSString+Normalize.h/m などのカテゴリを作成し、どちらかに正規化する。

NSString+Normalize.m
/**
 * "パ" to "ハ゜
 */
-(NSString*)noramlizeFormD {

	NSMutableString* _norstr = [NSMutableString stringWithString:self];
	CFStringNormalize((CFMutableStringRef)_norstr, kCFStringNormalizationFormD);

	return [NSString stringWithString:_norstr];
}
Example
str = [str noramlizeFormD];

#参考

10
9
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
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?