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?
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.
The contains() method returns: true - if the array contains the specified element. false - if the array doesn't contain the specified element.
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.
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."
Yes,
if things.contains(where: { $0.someProperty == "nameToMatch" }) { // found } else { // not }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With