Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSArray 和 NSMutableArray #103

Open
ShannonChenCHN opened this issue Jan 22, 2018 · 2 comments
Open

NSArray 和 NSMutableArray #103

ShannonChenCHN opened this issue Jan 22, 2018 · 2 comments

Comments

@ShannonChenCHN
Copy link
Owner

No description provided.

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Jan 22, 2018

如何根据数组中对象的条件来动态删除数组中元素?

比如,当要删除数组中属性 needsToBeDeleted 值为 YES 的对象时,如果直接一遍快速枚举一边删掉的话,会导致崩溃:

for (MyObject *anObject in array) {
       if (anObject.needsToBeDeleted) {
                [array removeObject: anObject];  // 会导致奔溃
       }
}

解决办法:

  • 逆序遍历
  • 现将要删除的对象的索引保存下来,然后再通过 removeObjectsAtIndexes 方法来删除元素。

参考

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Mar 24, 2018

NSArray/NSMutableArray 的实现

参考 :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant