23

Remove Object From Array Swift 3

I have a problem trying to remove a specific object from an array in Swift 3. I want to remove item from an array as in the screenshot but I don't know the solution.

If you have any solutions please share with me.

3
  • 1
    Please post actual code in your question, not an image. Commented Dec 21, 2016 at 5:09
  • you need to specify at which index the object is. Commented Dec 21, 2016 at 5:10
  • 1
    Please read the documentation for Array. It shows examples of adding and removing objects to/from an array. Commented Dec 21, 2016 at 5:12

3 Answers 3

37

Short Answer

you can find the index of object in array then remove it with index.

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

Long Answer

if your array elements confirm to Hashable protocol you can use

array.index(of: itemToRemove)

because Swift can find the index by checking hashValue of array elements.

but if your elements doesn't confirm to Hashable protocol or you don't want find index base on hashValue then you should tell index method how to find the item. so you use index(where: ) instead which asks you to give a predicate clouser to find right element

// just a struct which doesn't confirm to Hashable
struct Item {
    let value: Int
}

// item that needs to be removed from array
let itemToRemove = Item(value: 4)

// finding index using index(where:) method
if let index = array.index(where: { $0.value == itemToRemove.value }) {

    // removing item
    array.remove(at: index)
}

if you are using index(where:) method in lots of places you can define a predicate function and pass it to index(where:)

// predicate function for items
func itemPredicate(item: Item) -> Bool {
    return item.value == itemToRemove.value
}

if let index = array.index(where: itemPredicate) {
    array.remove(at: index)
}

for more info please read Apple's developer documents:

index(where:)

index(of:)

Sign up to request clarification or add additional context in comments.

4 Comments

Why bother with array.contains? Just get the index.
just to be sure :D but yes you can remove array.contains and just use if let index = array.index(of : itemToRemove)
@Mohammadalijf You don't need array.contains(itemToRemove) just if let index = array.index(of: itemToRemove) is enough
@Socheat It's right there in the documentation for the Swift Array class. index(of:).
20

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(...)
    }

The index existing means Array contains the object with that Tag.

2 Comments

@Socheat, 😄😄😄,the more you use Swift, the more you feel it powerful!
Yes, I think so bro.
6

I used the solutions provided here: Remove Specific Array Element, Equal to String - Swift Ask 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]"

Comments

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.