I have an array of objects like this:
var myArr = [
MyObject(name: "Abc", description: "Lorem ipsum 1."),
MyObject(name: "Def", description: "Lorem ipsum 2."),
MyObject(name: "Xyz", description: "Lorem ipsum 3.")
]
I know I can find the matched item like this:
var temp = myArr.filter { $0.name == "Def" }.first
But now how do I remove it from the original myArr? I was hoping the filter.first can return an index somehow so I can use removeAtIndex. Or better yet, I would like to do something like this:
myArr.removeAll { $0.name == "Def" } // Pseudo
Any ideas?
myArr = myArr.filter { $0.name != "Def" }?myArrvariable.myArris stillmyArr. And this is a value type; you cannot mutate it in place! You will always be creating a new array, even if you write a mutatingremoveAllmethod.removeAll()implementation is discussed here: codereview.stackexchange.com/questions/86581/….