|
Using SwiftData I have a very basic model with an optional date:
I'm working on the Add view to insert data into that model and no matter what I've tried so far, I get a variety of different errors around the date field. I'm fairly certain I have to hide the date picker, since I don't think I can have it displayed with an empty value. But even then, since I'm binding data to the field, I get this error:
I've tried a variety of alternates in the @State declaration around date with nil but those generate their own errors that It seems like it's impossible to have a date field that is optional but I've seen other apps pull it off, I simply have no idea how. Any suggestions? |
|
The initializer in your |
|
I think it does help, because things are starting to look right but I'm not quite all the way there yet. I updated the init in the model
And the State to align (I think): But I'm still getting the binding error at the date picker, which I haven't changed: |
|
The reason for error is the
A few questions for you
I have done a little hack to make it work. Change you
This will give Add two properites to hold the new infomation
Add a property to say if a date is you be used
Then you can use it in the
I have added a property for testing
Now add a funtion to change to a
For ease I added a
Lastly added a
I would serious consider seeing if I could just use non optional |
|
This is the complete demo project
|
|
Thank you so much! Unfortunatley, I do really need it to be an optional Date for other parts of the app to work correctly. This is unbelievably helpful. |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until September 29th.
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.