Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find multiple elements that are equal to a condition in array?

I am a new to SwiftUI, so I apologise if this a dumb question but I came upon a problem that I cant find a solution to.

I am searching something similar to this code below, but instead of finding the first element I would like to retrieve all elements that have the status == 0.

if let new = array.first(where: {$0.status == 0}) {
   // do something with foo
} else {
   // item could not be found
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Swift collections have a filter method which allows you to select all elements that match a given condition. You already have a matching condition, so to adapt your example text you’d write:

let newItems = array.filter { $0.status == 0 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading