Removing a specific object from an array in swift 3

Remove Object From Array Swift 3

I have a problem with deleting a specific object from an array in Swift 3. I want to remove an element from an array, as in the screenshot, but I do not know this solution.

If you have solutions, share with me.

+7
arrays swift3
source share
3 answers

you can find the index of the object in the array and then delete it using the index.

var array = [1, 2, 3, 4, 5, 6, 7] var itemToRemove = 4 if let index = array.index(of: itemToRemove) { array.remove(at: index) } 
+11
source share

According to your code, the improvement could be like this:

  if let index = arrPickerData.index(where: { $0.tag == pickerViewTag }) { arrPickerData.remove(at: index) //continue do: arrPickerData.append(...) } 

An existing index means that Array contains an object with this tag.

+6
source share

I used the solutions given here: Delete a specific array element equal to a string - quickly ask a question

this is one of the solutions there (in case the object was a string):

 myArrayOfStrings = ["Hello","Playground","World"] myArrayOfStrings = myArrayOfStrings.filter{$0 != "Hello"} print(myArrayOfStrings) // "[Playground, World]" 
+1
source share

All Articles