LoginSignup
41
39

More than 5 years have passed since last update.

[Objective-C] iOS8のUITextView.textに特殊文字を入れるとクラッシュする

Last updated at Posted at 2014-10-03

未だに不明なクラッシュ・・。
以下のようなコードだけでクラッシュします。
(ちなみにiOS7では問題なく起動できる)

[追記]
過去に似たような問題(Mac OS XとiOSで特定のアラビア語文字列を表示させるとクラッシュする問題が見つかる | スラッシュドット・ジャパン アップル)があった。
もしかしたらこれがiOS8で再燃したのかもしれない。

HogeViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *text = @"(੭ु´͈ ᐜ `͈)੭ु⁾⁾";
    CGRect frame = CGRectMake(200, 200, 150, 88);
    UITextView *view = [[UITextView alloc] initWithFrame:frame];
    view.text = text; // ←ここでクラッシュ!
    [self.view addSubview:view];
}

この顔文字の文字列をtextView.textに設定しようとするとクラッシュします。
文字列の並びを変えたりすると落ちないのでコードの並び?

ちなみにUITextFieldで試すとクラッシュしませんでした。
謎い・・。

[追記2]

社内のエンジニアがクラッシュする文字列を特定してくれました。
ただ、これも組み合わせなどでクラッシュしたりしなかったりなので確定ではなさそうです。

が、とりあえず組み合わせ文字のため、一括して消してしまっても見た目はそれほど変わらないため、iOSの対応が終わるまでは一括して消してしまってもいいかもしれません。

以下のようにすることでクラッシュしないことを確認できました。

non-crash
// クラッシュする文字
static NSString *text = @"(⌯˃̶᷄ ॣ˟ ॣ˂̶᷄⌯) ";

// 以下のunicodeを持つものを組み合わせるとクラッシュする
- (NSArray *)exceptUnicode
{
    return @[
        @"0900", @"0901", @"0902", @"0903",
        @"093a", @"093b", @"093c", @"093e", @"093f",
        @"0940", @"0941", @"0942", @"0943", @"0944", @"0945", @"0946", @"0947", @"0948", @"0949",
        @"094a", @"094b", @"094c", @"094d", @"094e", @"094f",
        @"0953", @"0954", @"0955", @"0956", @"0957",
        @"0962", @"0963"
    ];
}

// パターンを生成
NSMutableString *pattern = [NSMutableString string];
for (NSString *unicode in self.exceptUnicode) {
    [pattern appendString:[NSString stringWithFormat:@"\\u%@|", unicode]];
}


NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                       options:0
                                                                         error:nil];


NSString *t1 = text.mutableCopy;   
[regex replaceMatchesInString:t1
                      options:0
                        range:NSMakeRange(0, t1.length)
                 withTemplate:@""];
41
39
8

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
41
39