Remove matched item from array of objects?

前端 未结 4 1361
感动是毒
感动是毒 2020-12-11 17:56

I have an array of objects like this:

var myArr = [
  MyObject(name: \"Abc\", description: \"Lorem ipsum 1.\"),
  MyObject(name: \"Def\", description: \"Lore         


        
相关标签:
4条回答
  • 2020-12-11 18:39

    What you are not grasping is that Array is a struct and therefore is a value type. It cannot be mutated in place the way a class instance can be. Thus, you will always be creating a new array behind the scenes, even if you extend Array to write a mutating removeIf method.

    There is thus no disadvantage nor loss of generality in using filter and the logical negative of your closure condition:

    myArr = myArr.filter { $0.name != "Def" }
    

    For example, you could write removeIf like this:

    extension Array {
        mutating func removeIf(closure:(T -> Bool)) {
            for (var ix = self.count - 1; ix >= 0; ix--) {
                if closure(self[ix]) {
                    self.removeAtIndex(ix)
                }
            }
        }
    }
    

    And you could then use it like this:

    myArr.removeIf {$0.name == "Def"}
    

    But in fact this is a big fat waste of your time. You are doing nothing here that filter is not already doing. It may appear from the myArr.removeIf syntax that you are mutating myArr in place, but you are not; you are replacing it with another array. Indeed, every call to removeAtIndex in that loop creates another array! So you might as well use filter and be happy.

    0 讨论(0)
  • 2020-12-11 18:44

    Apple had added what you want in Swift 4:

    var phrase = "The rain in Spain stays mainly in the plain."
    let vowels: Set<Character> = ["a", "e", "i", "o", "u"]
    phrase.removeAll(where: { vowels.contains($0) })
    // phrase == "Th rn n Spn stys mnly n th pln."
    
    0 讨论(0)
  • 2020-12-11 18:44

    Get the objects using filter then loop through the array and use myArr.removeAtIndex(index) to remove each object. Using filter is doing exactly that. To understand what is happening read below. Matts answer is a much cleaner way to accomplish this since you're testing for the opposite match therefore each object is preserved unless it matches your value.

    Loop through your temp filter array

    if let index = find(temp, note) {
       myArr.removeAtIndex(index)
    }
    
    0 讨论(0)
  • 2020-12-11 18:44

    if you want to remove an object from an array that is being iterated, you should always iterate backwards as otherwise you will at some point work on indices that aren't valid anymore.

    var myArr = [
        ["key":"value1"],
        ["key":"value2"],
        ["key":"value3"]
    ]
    
    for index in stride(from: myArr.count - 1 , to: 0, by: -1){
        let x = myArr[index]
        if x["key"] == "value2"{
            myArr.removeAtIndex(index)
        }
    }
    
    0 讨论(0)
提交回复
热议问题