|
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:
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...
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:
And in your
where you would put the logic to figure out if the footer should be displayed or not. |
|
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:
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:
Same questions apply. |
|
It would be the same. Both
My preference (were this my code), would be to use If you use |
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.