-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?

Matias Jurfest
  • 1,378
  • 16
  • 25

3 Answers3

2

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

var storeArray = [obj1, obj2, obj3]
storeArray.removeAll(where: { $0 == obj2 })
Rob C
  • 4,877
  • 1
  • 11
  • 24
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 })
Matias Jurfest
  • 1,378
  • 16
  • 25
0

You can try this,

 var storeArray = [obj1, obj2, obj3]
 storeArray.remove(at: 1)
 print(storeArray)
Vandana pansuria
  • 764
  • 6
  • 14