|
I was fascinated to learn about the "missing" days in September 1752 on Day 26, and now on Day 28 I'm comfortable enough with DateComponents to mess around wth specific days. So of course I had to try and create one of those days in 1752. I put this into a playground:
And got the following result:
Shocking! I was totally expecting today, right now. So I'm fully on board with using Date and all the related stuff. (I highly doubt that September 1752 is going to figure strongly in my apps.) But my question (apart from "what gives?") is what are the actual cases when I might get a nil? I've been trying for a few minutes to break it but date(from: components) happily gives me something each time. Thanks, Geoff (@SwiftLearner3, currently Day 28) |
|
DateComponents have a lot of element eg. nanoseconds, era, etc and which are all optional. The complier need to make sure that they is aleast one to make the date(from: components) but will not know which so need to be unwrapped. If you want to try not to any components then make the date(from: components) and see the result. |
|
|
|
I just tried to create this small project to see if it would break the code by allowing me to enter non-number text values as the date components. But it still doesn't fail to create a date and default to
So yeah, I'm not sure why |
|
On second thought, my example tries to convert each So, it would be no different from setting all of the component property values equal to |
|
Even passing in all nils results in a non-nil Date coming back. I like your code, I modified it a little bit to remove the conversion from String to Int:
|
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.