Filter/ flatMap out objects with any nil values from array in swift 4

时间:2017-12-18 07:52:34

标签: arrays json swift

I am filling in a UICollectionView with results from a query. Sometimes, the resulting stories don't have images. I would like to skip those objects entirely.

How can I skip over results from a JSON query that don't have any image present? It looks like I should use flatMap, but can't get it going.

Here is the code I'm using:

<a href="#">Test</a>

1 个答案:

答案 0 :(得分:1)

There are two ways to solve this:

  • Don't append the article if the Value Objects nil

  • Append everything, but then filter the items with image only:

    let hasImageArticles = articles.filter({ $0.imageURL != nil })