admin管理员组文章数量:1415645
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)")
}
}
本文标签: swiftuiHow to filter data with budget ranges like 39Under 5039 in SwiftStack Overflow
版权声明:本文标题:swiftui - How to filter data with budget ranges like 'Under $50' in Swift? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745240818a2649318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论