Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Check if array contains element with property [duplicate]

Tags:

arrays

swift

I have an array of custom objects.

I want to check if array contains an object, which property is equal to string.

Something like

 if array.contains(object where object.name == name) {      // do something  } else {      // don't do something  } 

How to do in Swift?

like image 689
Alexey K Avatar asked Jun 23 '16 10:06

Alexey K


People also ask

How do you check if an array contains a duplicate value?

To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.

How do you check if an array contains an item Swift?

The contains() method returns: true - if the array contains the specified element. false - if the array doesn't contain the specified element.

Which property that used to check an array has items in Swift?

Use the isEmpty property to check quickly whether an array has any elements, or use the count property to find the number of elements in the array.

How do you find the duplicate number on a given integer array in Swift?

The easiest way to find duplicate elements is if the phone number is just a 6-digit number and has type Int, you could sort the array of phone numbers and than filter that to find duplicates. And so on. @Mazyod - from the answer: "you could SORT the array of phone numbers and than FILTER that to find duplicates."


1 Answers

Yes,

if things.contains(where: { $0.someProperty == "nameToMatch" }) {      // found } else {      // not } 
like image 54
DogCoffee Avatar answered Oct 04 '22 00:10

DogCoffee