简体   繁体   English

如何删除数组列表中数组的所有元素

[英]How to delete all element of an array in a list of arrays

I have a list of arrays look like this:我有一个数组列表,如下所示:

var lists = [[Category]]()

Each element in the "lists" represents an array of object (Category), I'm trying to reinit the "lists" (clear every element) after using it. “列表”中的每个元素都代表一个对象数组(类别),我试图在使用它后重新初始化“列表”(清除每个元素)。

My approach:我的做法:

lists.forEach { list in 
    list.removeAll()
}

Error: Can not mutate the "list" because "list" is a constant variable错误:无法改变“列表”,因为“列表”是一个常量变量

Is there anyway I can achieve my goal in Swift?无论如何,我可以在 Swift 中实现我的目标吗?

Since arrays are value types and they have copy-on-write semantics, "clearing all the nested arrays of an array" is indistinguishable from "creating a new array with as many empty nested arrays as there are nested arrays in the old array, and reassigning it to the old array".由于数组是值类型并且它们具有写时复制语义,因此“清除数组的所有嵌套数组”与“创建具有与旧数组中嵌套数组一样多的空嵌套数组的新数组,以及将其重新分配给旧数组”。

So you can do所以你可以做

lists = Array(repeating: [], count: lists.count)

Alternatively:或者:

lists = lists.map { _ in [] }

From the comments, it seems like you are writing a function that clears all the nested arrays.从评论来看,您似乎正在编写一个清除所有嵌套数组的函数。 In that case, you need an inout parameter:在这种情况下,您需要一个inout参数:

func clearList(_ lists: inout [[Category]]) {
    lists = Array(repeating: [], count: lists.count)
}

And to call it:并称之为:

var myLists = [listCate1, listCate2]
clearList(&myLists)
// myLists will be [[], []], note that this does not change listCate1 or listCate2

If you really want your function to mutate an arbitrary number of lists that you pass in, you'd need to use unsafe pointers (very not recommended):如果你真的想让你的函数改变你传入的任意数量的列表,你需要使用不安全的指针(非常不推荐):

func clearList(_ lists: UnsafeMutablePointer<[Category]>...) {
    for list in lists {
        list.pointee = []
    }
}

And clearList(&listCate1, &listCate2) would actually change listCate1 and listCate2 , but this is a rather dirty trick.clearList(&listCate1, &listCate2)实际上会改变listCate1listCate2 ,但这是一个相当肮脏的把戏。

You can assign an empty array literal which is written as [ ] (an empty pair of square brackets)您可以分配一个写为 [] 的空数组文字(一对空方括号)

lists = []

lists is now an empty array, but still of type [[Category]]列表现在是一个空数组,但仍然是[[Category]]类型

You can remove all the element of an array and keep the capacity.您可以删除数组的所有元素并保留容量。

lists.removeAll(keepingCapacity: true)

or if you want to remove object from array based on some conditions then you can use this...或者如果您想根据某些条件从数组中删除对象,那么您可以使用它...

lists.removeAll { (obj) -> Bool in

   if <check some condition {   return true  }
   else { false  }
   
 }
    var someInts1 = [30,11,34]
    var someInts2 = [30,11,34]
    var someInts3 = [30,11,34]

    var lists = [Any]()
    lists = [someInts1, someInts2, someInts3]
    print(lists) // [[30,11,34], [30,11,34], [30,11,34]]

    lists = [] // this is where the list become empty
    print(lists) // []

I'd create an extension with mutable and immutable variants.我会创建一个带有可变和不可变变体的扩展。 By extending RangeReplaceableCollection rather than Array we don't need to constrain Array.Element to any specific type.通过扩展RangeReplaceableCollection而不是Array我们不需要将Array.Element限制为任何特定类型。

extension RangeReplaceableCollection where Element: RangeReplaceableCollection {
    func stripped() -> Self {
        return .init(repeating: .init(), count: count)
    }

    mutating func strip() {
        self = stripped()
    }
}

Now we can strip in place if our array is declared mutable:现在,如果我们的数组被声明为可变的,我们可以就地剥离:

var mutable: [[Category]] = []
mutable.strip()

Or we can use the immutable variant for let constants:或者我们可以对let常量使用不可变变量:

let immutable: [[Category]] = []
let stripped = immutable.stripped()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM