-2

I have array of objects

let storeArray = [obj1, obj2, obj3]

And I want to remove obj2, how can I remove this with Swift 5?

1
  • 1
    Did you try anything, have you done any research like reading the documentation for Array? Commented Apr 3, 2020 at 13:17

3 Answers 3

2

If you specifically want to remove obj2, you can do this...

var storeArray = [obj1, obj2, obj3]
storeArray.removeAll(where: { $0 == obj2 })
Sign up to request clarification or add additional context in comments.

Comments

1

If you know that obj2 is in index 1 of the array and you trust that:

var array = [obj1, obj2, obj3]
guard array.count > 1 else { return }
array.remove(at: 1)

If you want to remove the obj2 without trusting it's index:

var array = [obj1, obj2, obj3]
array.removeAll(where: { $0 == obj2 })

Comments

0

You can try this,

 var storeArray = [obj1, obj2, obj3]
 storeArray.remove(at: 1)
 print(storeArray)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.