Alexey K Alexey K - 5 months ago 9
Swift Question

Swift - check if array contains element with property

I have an array of custom objects.

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

Somethin like

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


Is it possible to do in swift ?

Answer

Yes,

if things.contains{ $0.someProperty == "nameToMatch" } {
    // found
}
else {
    //  not
}
Comments