0

I am working in array handling , the overall working of the application is the user can select the number of number of songs that has to be added to a particular playlist. I have used Tableview to list the songs when the user click the tableview cell have added .checkmark when already checkmark found I will remove .checkmark, similarly when the user select the song I have kept a array to save the track id of that particular song . "SongIDArray" is the array used to save. When the user deselect the song I have to delete the particular songs "track id". but in array it has remove(at: index) I cannot delete it through index. please help me

for example :** my array is**

songIDArray = [25,45,69,78,63]

I need to remove 78 from the index without knowing the index path.

3 Answers 3

1

You can use filter to remove values from an array:

let newArray = [25, 45, 69, 78, 63].filter { $0 != 78 }
Sign up to request clarification or add additional context in comments.

2 Comments

can you elaborate your answer, i.e I need to remove 78 ,but array has only index to remove how should i remove this
@KarthickTM You use it like this: let newArray = songIDArray.filter { $0 != 78 }
1

removeAll is the command for conditionally removing.

var array = [25,45,69,78,63]
array.removeAll{$0 == 78}
print(array)

//[25, 45, 69, 63]

Comments

0

You have a few options here, whichever is best suited to your exact use-case!

Remove by filtering

The filter function passes each element in the array to the block of code you provide and removes all items you return false for. This is useful for performing a simple filter, but if you also need the index of the removed item (For example to reload a table view index path) it's not very useful.

songIDArray = songIDArray.filter({ id in
    return id != 78
})

Remove by index

First get the index to remove at, you can do this in various ways depending on the type of items you have in your array. If they conform to Equatable (Integers do) you can use

if let removalIndex = songIDArray.firstIndex(of: 78) {
    songIDArray.remove(at: removalIndex)
}

If you don't have an array of Equatable elements, then you can instead use index(where:) which returns the first index for which you return true from the provided closure:

if let removalIndex = songIDArray.index(where: { id in
    return id == 78
}) {
    songIDArray.remove(at: removalIndex)
}

This method is useful if you also need to do something else with the index, like reload a UITableViewCell at a specific index path to reflect your change e.t.c.

Remove All

In swift 4.2 there is also a removeAll(where:) function which works similar to filter but doesn't return a new array:

songIDArray.removeAll(where: { id in
    return id == 78
})

2 Comments

what happens when the array is the type of NSMUTABLEARRAY()
Is there any particular reason you have an NSMutableArray and not a swift array? I believe the it should be cast-able to a swift array anyways (Although I haven't tried it)!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.