Swift version: 5.2
Swift uses exclamation marks to signal both force unwrapping of optionals and explicitly unwrapped optionals. The former means "I know this optional variable definitely has a value, so let me use it directly." The latter means "this variable is going to be nil initially then will definitely have a value afterwards, so don't make me keep unwrapping it."
Broadly speaking, using exclamation marks is frowned upon because "trust me it's safe" isn't as good as the compiler absolutely enforcing it. That being said, it's your code: if you know something cannot be nil (usually because if it were nil your program would explode!) then do what works best.
SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.
Available from iOS 7.0 – see Hacking with Swift tutorial 1
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.