Dave F Dave F - 1 year ago 185
Swift Question

How would I refactor the following code to use NSSet.objectsPassingTest()

I have the following code in my ViewController. I'd like to refactor it to use

so it's more concise and easier to follow. I'm using Swift, so no Objective C.

let items = feed.items?.allObjects as! [Item]
var unreadCount = 0
for item in items {
if !item.read {
unreadCount = unreadCount + 1

I've spent a few hours searching, but I have not been able to find an example that I can understand.

In case it's not clear, I'd like to replace
let items = feed.items?.allObjects as! [Item]
let items = feed.items?.objectsPassingTest()

Answer Source

Another approach

let unreadCount = items.filter { !$0.read }.count
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download