So I have an array of Int's and I am trying to create a function to delete a certain value from the array. However, the removeAtIndex() function only deletes the first occurrence and the removeLast() only removes the last. I tried enumerating through the array but I end up with an Array index out of range error, possible due to the reshifting that occurs when deleting an item from the array.
for (index, value) in connectionTypeIDs.enumerate() {
if (value == connectionTypeToDelete){
connectionTypeIDs.removeAtIndex(index)
}
}
Any ideas on how to accomplish this?
Using Array. The splice() method in JavaScript is often used to in-place add or remove elements from an array. The idea is to find indexes of all the elements to be removed from an array and then remove each element from the array using the splice() method.
To create an empty string array in Swift, specify the element type String for the array and assign an empty array to the String array variable.
Use a dictionary like var unique = [<yourtype>:Bool]() and fill in the values like unique[<array value>] = true in a loop. Now unique.
The quickest way is to use filter. I don't know is that answer your question but you can have a look on this:
// remove 1 from array
arr = arr.filter{$0 != 1}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With