|
In the post How can I search in different columns in SwiftData Query? @ygeras pointed out to me that I should better use Enum for the values I use in the picker. I have now tried this to learn more, but fail in the init method. I have implemented the following code to use enums: The enum definition:
In the "ChargingExpensesView" view:
The init method in "ChargingExpensesListingView": Attempts 1:
Here comes the error: Member access without an explicit base is not allowed in this predicate Attempts 2:
Here comes the error: Switch expressions are not supported in this predicate Does anyone have an idea what I'm doing wrong here, or is my whole enum approach incorrect? I am grateful for any tips. |
|
Hi @ralfb1105! Yesterday, I myself wanted to see who enums work in Predicate and it seems it is more pain than gain, well seems at the time being at least. Swift data is still quiet fresh so I guess this will change sooner or later. For me the reason to use enum in this particular case is to avoid typing in Strings as you may misspell it sooner or later. If you project contains not so many fields to search I guess you may go without using enums, but if projects starts to expand you may think of using enums in that case. As said above, with just two search options it maybe much easier to use just strings instead of enums. |
|
Hi @ygeras, thank you again for your assessment. If it's not so easy at the moment, then I'm reassured that I haven't made a big mistake in principle. For my app it is currently not necessary because I only have two fields, and I can easily make sure that I don't make any mistakes :-) I will certainly try it again later and maybe SwiftData and Swift will be ready by then so that it is a bit easier. |
|
On the second thought though, we can use enums ))) but initializing the search in slightly different way. Let's assume you create enum as following but definitely change to your case:
Then create @State var to hold that value:
Use Menu as previously but your way:
Then pass the selection to your DestinationListingView as before:
BUT THEN in the latter view you initialize it AS:
SO now you can use switch and can use enum without the need to utilize it with PREDICATE ))). Seems to be working just fine, did not check all use cases though! |
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!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.