https://jacobbartlett.substack.com/p/2-minute-tips-the-dark-secret-of [https] Jacob's Tech Tavern SubscribeSign in Share this post [https] 2 Minute Tips: The Dark Secret of Optionals jacobbartlett.substack.com Copy link Facebook Email Note Other 2 Minute Tips: The Dark Secret of Optionals They aren't what you think. [https] Jacob Bartlett Oct 02, 2023 2 Share this post [https] 2 Minute Tips: The Dark Secret of Optionals jacobbartlett.substack.com Copy link Facebook Email Note Other Share Psst! Come here, I'll let you in on a secret. [https] Photo by Dan LeFebvre on Unsplash Question marks, exclamation marks, chaining, coalescing? Optionals must be some Swift language magic, right? You couldn't be further from the truth. Open Xcode and type this in: Optional.none() Now, quick, before anyone sees you! CMD+Click on the word Optional. Press enter. [https] After this, there is no turning back. You close Xcode -- the story ends, you wake up in your bed and believe whatever you want to believe. You command-click the optional -- you stay in Wonderland and I show you how deep the rabbit hole goes. Like breaking out of the Matrix, we finally see the code that makes up our world -- the world of the the Swift Standard Library. The source code for Optional. @frozen public enum Optional: ExpressibleByNilLiteral { case none case some(Wrapped) } Your whole life was a lie. Optionals were a 2-case enum the entire time. It's true. Optionals are just part of the Swift Standard Library. The ?, !, and ?? you know and love? Syntax sugar, my friend. As fentanyl is to heroin; the ? syntax gives a stronger kick at a much lower dose than Optional. The Swift Compiler metabolises it into the same end product: Getting high on type-safe nullability. Let's end with a use case for this forbidden knowledge. Imagine you need to store a flag in User Defaults, or, heaven forbid, the Keychain, to signify whether the user has been through your app onboarding. Your navigation logic might look like this: if let hasSeenOnboarding = UserDefaults.standard.value( forKey: "hasSeenOnboarding" ) as? Bool, !hasSeenOnboarding { showOnboarding() } } else { showOnboarding() } With an explicitly typed optionals, you can simply write: if UserDefaults.standard.value( forKey: "hasSeenOnboarding" ) != Optional.some(true) { skipOnboarding() } p.s. yes, I know that bool(forKey:) exists on UserDefaults already. Jacob's Tech Tavern is a reader-supported publication. If you want to read more, please consider becoming a free subscriber. If you want me to write more, please consider becoming a paid subscriber! [ ] Subscribe 2 Share this post [https] 2 Minute Tips: The Dark Secret of Optionals jacobbartlett.substack.com Copy link Facebook Email Note Other Share Comments [https] [ ] Top Latest Discussions No posts Ready for more? [ ] Subscribe (c) 2024 Jacob Bartlett Privacy [?] Terms [?] Collection notice Start WritingGet the app Substack is the home for great culture Share Copy link Facebook Email Note Other This site requires JavaScript to run correctly. Please turn on JavaScript or unblock scripts