LoginSignup
3
3

More than 5 years have passed since last update.

NSMutableArrayの要素を削除

Posted at

NSMutableArray型び配列の要素を削除している最中に、配列にアクセスすると「Collection was mutated while being enumerated」という例外が出る。

これを解決するためには逆順からアクセスする列挙子「reverseObjectEnumerator」を用いればよい。

リンクリスト構造の配列は「次の要素」情報を管理しているので、逆から削除するとこれからアクセスする要素には影響がないかららしい。

for (id object in array) {
    // objectを削除する必要があるかどうかチェックする
    if ([object shoudRemove]) {
        // objectを削除する
        [array removeObject:object];
    }
}

参考:ダイナミックObjective-C

3
3
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
3
3