最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swiftui - How to filter data with budget ranges like 'Under $50' in Swift? - Stack Overflow

programmeradmin1浏览0评论

My filter logic works correctly for the matchesPlatform variable. If I try to also filter by the budget range..my print statemetn fires off for example on my under $50 case in my switch statement, but the campaigns do not show up in my view

The budget values are all integers and are pulling correctly. Filter Logic

  var filteredCampaigns: [CampaignPostModel] {
    // If no filters are selected, return all campaigns
    if selectedFilters.isEmpty {
        return localCampaignManager.allCampaigns
    } else {
        return localCampaignManager.allCampaigns.filter { campaign in
            // Budget Filter Logic (convert string to numeric comparisons)
            let budgetValue = campaign.budget // Assuming budget is a numeric value, not a string
            var matchesBudget = true // Default to true if no budget filter is applied

            // Check if the campaign's socialLinks contains all of the selectedFilters (all selected platforms must be present)
            let matchesPlatform = selectedFilters.allSatisfy { selectedFilter in
                campaign.socialLinks.contains(selectedFilter)
            }

            // Check if selectedFilters contain any budget-related values
            for filter in selectedFilters {
                switch filter {
                case "Under $50":
                    // Only exclude campaigns where the budget is 50 or above
                    if budgetValue <= 50 {
                        print("This statement fires off")
                        matchesBudget = true
                    }
                case "$50-$100":
                    // Check if the budget is between 50 and 100 (inclusive of 50 and exclusive of 100)
                    if budgetValue < 50 || budgetValue >= 100 {
                        matchesBudget = false
                    }
                case "$100-$499":
                    // Check if the budget is between 100 and 499
                    if budgetValue < 100 || budgetValue >= 500 {
                        matchesBudget = false
                    }
                case "$500 and above":
                    // Only exclude campaigns where the budget is less than 500
                    if budgetValue < 500 {
                        matchesBudget = false
                    }
                default:
                    break // Ignore other filters for budget
                }
            }

            // Return campaigns that match both the platform filter and the budget filter
            return matchesPlatform && matchesBudget
        }
    }
}

View:

  ForEach(filteredCampaigns, id: \.id) { campaign in
                                NavigationLink(
                                    destination: CampaignListingMain(
                                        campaignName: campaign.title,
                                        budget: String(campaign.budget),
                                        influencerFocus: campaign.focus,
                                        campaignDetails: campaign.description,
                                        selectedPlatforms: campaign.socialLinks
                                    ),
                                    tag: campaign.id,
                                    selection: $selectedCampaignID
                                ) {
                                    ConnectListingRow(
                                        title: campaign.title,
                                        subtitle: campaign.focus,
                                        socialLinks: campaign.socialLinks,
                                        price: "$\(campaign.budget)"
                                    )
                                }
                                .id(campaign.id)
                                .buttonStyle(PlainButtonStyle())
                                .onAppear{
                                    print("Camapign budget: \(campaign.budget)")
                                }
                            }
发布评论

评论列表(0)

  1. 暂无评论