ios - iOS Swift Is it worth trying to remove objects from a Swift array when filtering?
问题描述
I have a table view with 30 rows. There are 10000 data points that are ordered chronologically. Each table view cell pulls out about 300 data points based on a filter clause. I have not seen the equivalent of "removeObjectsInArray" for Swift arrays removeObjects(in:)
- Do I need to convert/cast swift array into NSMutableArray to be able to remove objects from array?
- Is it worth trying to remove objects from swift array?
I'm not sure if Swift's treatment of arrays would re-allocate the entire array, causing a bigger performance hit then gained by reducing the array size.
解决方案
最好不要做一些强制转换来做这些事情,这可以通过纯粹的快速方法来完成。
这是一个例子:
var mainArraySwift: [String] = []
mainArraySwift.append("a")
mainArraySwift.append("b")
mainArraySwift.append("c")
mainArraySwift.append("d")
mainArraySwift.append("e")
print(mainArraySwift)
var arrayToBeRemoved: [String] = []
arrayToBeRemoved.append("a")
arrayToBeRemoved.append("b")
arrayToBeRemoved.append("c")
print(arrayToBeRemoved)
mainArraySwift.removeAll { (value) -> Bool in
return arrayToBeRemoved.contains(value)
}
print(mainArraySwift)
输出是:
["a", "b", "c", "d", "e"]
["a", "b", "c"]
["d", "e"]
与 NSMutableArray 的方法不完全匹配,但这是您感兴趣的事情。
推荐阅读
- rxjs - 用大理石图测试去抖动
- angularjs - 在 angular.js 中包含带有 $provide.factory 的工厂函数后无法调用工厂方法
- javascript - 将参数传递给 Function Express JS
- javascript - 来自嵌套函数的Vuejs访问函数
- javascript - sequilize:如何获取创建时间超过 1 小时的记录
- javascript - 承诺不会将所有数据提取到前端
- php - 将数组转换为对象的更好方法
- algorithm - 选择序列中的元素
- bash - 在后台运行 mplayer 命令将 &1 替换为 1
- bash - 通过比较多个字符串来查找和删除文件中的行