NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

SOLVED: Conditionals in a list

Forums > SwiftUI

Hi. I'm hoping someone here can point me in the right direction with this. I have an array of predefinedItems of a custom DateInterval struct like this:

let predefinedItems: [DateInterval] = [DateInterval(name: "30 Day", value: 30, scope: scopeIntFor("Day")),
                                           DateInterval(name: "60 Day", value: 60, scope: scopeIntFor("Day")),
                                           DateInterval(name: "90 Day", value: 90, scope: scopeIntFor("Day")),
                                           DateInterval(name: "3 Month", value: 3, scope: scopeIntFor("Month")),
                                           DateInterval(name: "6 Month", value: 6, scope: scopeIntFor("Month")),
                                           DateInterval(name: "9 Month", value: 9, scope: scopeIntFor("Month")),
                                           DateInterval(name: "1 Year", value: 1, scope: scopeIntFor("Year")),
                                           DateInterval(name: "2 Year", value: 2, scope: scopeIntFor("Year")),
                                           DateInterval(name: "3 Year", value: 3, scope: scopeIntFor("Year"))]

In body below that I have a GroupListStyle() list made up of 3 sections. The first has a button named of "None". The second is a ForEach going through my predefinedItems and in that I have a HStack with a button showing the name, and a Spacer(), and an Image with the systemName "checkmark". The third has HStack with a NavigationLink with a label name of "Custom" and also an Image with the systemName "checkmark" and the section itself also has a footer that could contain some information (or not). So here's what I'm trying to do...

I have a var selection: String (passed in from the view that calls this view in a NavigationLink). If the string contains None, I would like to conditionally hide all the checkmarks. If the string contains a name in my predefinedItems, I want to show the checkmark only next to that item. If the string contains something else that isn't None and doesn't match any of the item names in predefinedItems, I would like to display the checkmark next to my Custom item and display the selection string in the footer.

The second section was easy enough since I'm looping through predefinedItems in a ForEach and getting one preItem at a time... so in my HStack...

                            if selection == preItem.name {
                                Image(systemName: "checkmark")
                                    .foregroundColor(.accentColor)
                                    .font(.headline)
                            }

So if it is "None"... there is no checkmark displayed in the second section of my list since "None" doesn't match on any of the names in the array. If selection holds something like "1 Year" then the checkmark only appears in my list next to "1 Year"... perfect up to this point.

The problem comes in with my last section. I need a way to say if the selection isn't "None" or isn't in the names in the predefinedItems array then I want it to put the selection string in my footer for that third section and I want it to display my checkmark image. I thought maybe I could use an @State private var isCustomItem = true at the top and then maybe in the conditional in my second section as I'm looping through my predefinedItems I could set it to false if there is a match and I show the checkmark there... then at the bottom section I could check if the selection is "None" or if the isCustomItem is false and if not, then I could display my footer text (passing along the selection there) and display the checkmark image. But that doesn't work: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols

For example, if the selection is "45 Day"... that isn't "None" and it isn't a name in my predefinedItems array... so I would want to put the "45 Day" into the footer of the third section and make sure my checkmark image is displayed because "45 Day" must be a custom item.

I know there must be some way to express this in SwiftUI, but I'm drawing a blank at the moment.

   

It's a little hard to understand with so much text and so little code, but...

You can do something like this for your footer:

if shouldShowFooter {
    //...place your footer stuff here
}

And in your View, you would need a computed property like so:

var shouldShowFooter: Bool {
    !predefinedItems.contains { $0.name == selectedItem } && selectedItem != "None"
}

where you would put the logic to figure out if the footer should be displayed or not.

1      

Thanks @roosterboy! That's exactly what I was missing. I either forgot (likely), or didn't realize (possibly) that I could use contains with $0.name to dig down to what I needed.

Sorry about the shortness of code I provided, it is honestly a bit of a mess right now as I'm still working my way through a lot of it and trying out different interface ideas and such as I get into a mindset of iOS and iPadOS UI, so I was trying not to post all the stuff that would distract from the particular part I was trying to figure out at the moment.

Actually, now that you pointed me in this direction... I wonder if I could use something like this instead of contains:

var shouldShowFooter: Bool {
    !predefinedItems.first(where: { $0.name == selectedItem }) && selectedItem != "None"
}

In theory, that would be faster because it would stop at the first match it finds (if there is one) and not keep going (correct?) and wouldn't that offer better protection against a match like... "11 Year" contains "1 Year" where == would need to be an exact match? I suppose in practice in terms of speed it wouldn't really matter here because we are only talking about going through 9 predefined items in an array.

   

Oops, Xcode didn't like that when I tried it... had to change it to:

var shouldShowFooter: Bool {
    (predefinedItems.first(where: { $0.name == selectedItem }) == nil) && selectedItem != "None"
}    

Same questions apply.

   

In theory, that would be faster because it would stop at the first match it finds (if there is one) and not keep going (correct?) and wouldn't that offer better protection against a match like... "11 Year" contains "1 Year" where == would need to be an exact match?

It would be the same. Both contains(where:) and first(where:) have O(n) time complexity. They both stop and return at the first match.

first(where:) is good for when you need to know the position in the array of an element; contains(where:) is for when you just need to know if it's in there somewhere but you don't care where exactly it is.

My preference (were this my code), would be to use contains(where:) simply because it returns a Bool, which is what is needed, and I wouldn't have to do any checking for nil. And because all that matters is whether or not the selectedItem is in there; where it is in the array doesn't really matter.

If you use !predefinedItems.contains { $0.name == selectedItem } it will never match "11 Year" if you ask it for "1 Year". contains in this instance means "does the array contain an item whose name matches selectedItem?", not "does any element of the array contain that value within itself". You aren't testing $0.name.contains(selectedItem).

1      

Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

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.