TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Trouble with Filter() and .contains()

Forums > SwiftUI

Hi all,

I'm working to implement something we learned during the Hot Prospects project.

I'd like to implement dynamic filtering on a set of trending TV data received from an API by country - ideally I would have a drop down that would allow users to select the trending TV shows in their country of choosing. I've pasted a simplified version of my code below - essentially, I'm noticing that adding a !.contains("US") filter works as expected (no shows that contain "US" in their origin array make it through), but simply a .contains("US") filter does not. Is there some nuance that I'm missing? This is true if I filter on something other than origin country as well - name, for instance. There's got to be some boolean logic that I'm missing here, but it seems like what the filter would deem 'true' would just change between the two versions of .contains statements. What am I overlooking?

import SwiftUI

struct GridViewPostersTrendingTest: View {
    enum FilterType {
        case none, country
    }

    @State private var trendingShows = [TrendingShows.example]

    let filter: FilterType

    var body: some View {
        ScrollView {
            LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 4), content: {
                ForEach(filteredProspects.removingDuplicates()) { series in
                    NavigationLink {
                        SeriesView(tvId: series.id)
                    } label: {
                        VStack {
                            GridSeriesPosterHyp(image: series.posterPath ?? "", name: series.name ?? "")
                        }
                        .padding(.bottom, 4)
                    }
                }
            })
        }
        .navigationBarTitle("Trending Shows (US)", displayMode: .inline)
    }

    var filteredProspects: [TrendingShows] {
        switch filter {
        case .none:
            return trendingShows
        case .country:
            return trendingShows.filter { (!($0.originCountry ?? []).contains("US")) }
        }
    }
}

Sample of the JSON I'm looking at below:

[
    {
        "id": 114461,
        "name": "Ahsoka",
        "origin_country": [
            "US"
        ]
    },
    {
        "id": 205715,
        "name": "Gen V",
        "origin_country": [
            "US"
        ]
    },
    {
        "id": 72710,
        "name": "The Continental: From the World of John Wick",
        "origin_country": [
            "US"
        ]
    },
    {
        "id": 84958,
        "name": "Loki",
        "origin_country": [
            "US"
        ]
    },
    {
        "id": 37854,
        "name": "One Piece",
        "origin_country": [
            "JP"
        ]
    }
]

2      

Are you saying that "One Piece" still shows up in the results when you remove the ! from your code, even though its origin country is "JP"?

(If so, that would be unexpected behavior.)

Or are you saying that removing the ! from your code makes all of the shows that have "US" as the origin country show in your results, but it doesn't show "One Piece"?

(If it's this second option, that is expected behavior.)

The code that you are currently using basically says 'Only show results that do NOT have the string "US" in their origin country.' So, it should only show "One Piece".

Removing the ! from your code would basically be saying 'Only show results that do have the string "US" in their origin country.' So, it should show everything except "One Piece".

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.