Dave F Dave F - 6 months ago 33
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

NSSet.objectsPassingTest()
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]
with
let items = feed.items?.objectsPassingTest()
.

Answer

Another approach

let unreadCount = items.filter { !$0.read }.count