在 Swift 中从 NSMutableArray 中选择性地删除和删除对象 [英] Selectively remove and delete objects from a NSMutableArray in Swift

查看:78
本文介绍了在 Swift 中从 NSMutableArray 中选择性地删除和删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本问题.在 Swift 中从可变数组中选择性地移除和删除项目的最佳方法是什么?

Basic question. What is the best way to selectively remove and delete items from a mutable Array in Swift?

有些选项似乎不适合这种情况,例如在

There are options that do NOT seem to be suited for this like calling removeObject inside a

  • for 循环
  • 枚举块

和其他看起来一般般的工作

and others that appear to work in general like

  • for 循环 使用索引 + 调用 removeObjectAtIndex,甚至在循环内部
  • for in loop 用于填充 arrayWithItemsToRemove 然后使用 originalArray.removeObjectsInArray(arrayWithItemsToRemove)
  • 使用 .filter 创建一个新数组似乎非常好,但我不太确定替换整个原始数组的感受
  • for loop using an index + calling removeObjectAtIndex, even inside the loop
  • for in loop for populating an arrayWithItemsToRemove and then use originalArray.removeObjectsInArray(arrayWithItemsToRemove)
  • using .filter to create a new array seems to be really nice, but I am not quite sure how I feel about replacing the whole original array

是否有推荐的、简单且安全的方法来从数组中删除项目?我提到的那些或我遗漏的东西之一?

Is there a recommended, simple and secure way to remove items from an array? One of those I mentioned or something I am missing?

对此有不同的看法(有利有弊)或偏好会很好.我仍然很难选择正确的.

It would be nice to get different takes (with pros and cons) or preferences on this. I still struggle choosing the right one.

推荐答案

如果你想根据条件循环并从 NSMutableArray 中移除元素,你可以以相反的顺序循环数组(从最后一个索引为零),并删除满足条件的对象.

If you want to loop and remove elements from a NSMutableArray based on a condition, you can loop the array in reverse order (from last index to zero), and remove the objects satisfying the condition.

例如,如果您有一个整数数组并且想要删除可被 3 整除的数字,您可以像这样运行循环:

For example, if you have an array of integers and want to remove the numbers divisible by three, you can run the loop like this:

var array: NSMutableArray = [1, 2, 3, 4, 5, 6, 7];

for index in stride(from: array.count - 1, through: 0, by: -1) {
    if array[index] as Int % 3 == 0 {
        array.removeObjectAtIndex(index)
    }
}

以相反的顺序循环确保仍要检查的数组元素的索引不会改变.相反,在前向模式下,如果您删除例如第一个元素,那么之前位于索引 1 的元素将更改为索引 0,您必须在代码中考虑这一点.

Looping in reverse order ensures that the index of the array elements still to check doesn't change. In forward mode instead, if you remove for instance the first element, then the element previously at index 1 will change to index 0, and you have to account for that in the code.

出于性能原因,不建议在循环中使用 removeObject(不适用于上述代码),因为它的实现循环遍历数组的所有元素并使用 isEqualTo 决定是否移除对象.复杂度顺序从 O(n) 增加到 O(n^2) - 在最坏的情况下,数组的所有元素都被删除,数组在主循环中遍历一次,并再次遍历数组的每个元素大批.所以所有基于枚举块、for-in 等的解决方案都应该避免,除非你有充分的理由.

Usage of removeObject (which doesn't work with the above code) is not recommended in a loop for performance reasons, because its implementation loops through all elements of the array and uses isEqualTo to determine whether to remove the object or not. The complexity order raises from O(n) to O(n^2) - in a worst case scenario, where all elements of the array are removed, the array is traversed once in the main loop, and traversed again for each element of the array. So all solution based on enumeration blocks, for-in, etc., should be avoided, unless you have a good reason.

filter 反而是一个不错的选择,我会使用它,因为:

filter instead is a good alternative, and it's what I'd use because:

  • 简洁明了:1 行代码,而不是基于索引的解决方案的 5 行(包括右括号)
  • 它的性能与基于索引的解决方案相当 - 它有点慢,但我认为没有那么多

不过,它可能并非在所有情况下都理想,因为正如您所说,它会生成一个新数组,而不是原地操作.

It might not be ideal in all cases though, because, as you said, it generates a new array rather than operating in place.

这篇关于在 Swift 中从 NSMutableArray 中选择性地删除和删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆