Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Swift check if array contains object

Tags:

arrays

swift

I have this array :

  var preferiti : [ModalHomeLine!] = []

I want to check if the array contains the same object.

if the object exists {

} else {
  var addPrf = ModalHomeLine(titolo: nomeLinea, link: linkNumeroLinea, immagine : immagine, numero : titoloLinea)
  preferiti.append(addPrf)
}
like image 916
Massimiliano Allegretti Avatar asked Nov 05 '25 11:11

Massimiliano Allegretti


1 Answers

Swift has a generic contains function:

contains([1,2,3,4],0) -> false
contains([1,2,3,4],3) -> true
like image 91
erdekhayser Avatar answered Nov 08 '25 14:11

erdekhayser