A concise, runnable playground example of implicitly unwrapped optionals in Swift.
An implicitly unwrapped optional (declared with a trailing !, e.g. String!) is an optional that Swift automatically unwraps whenever you access it, so you can use its value without writing ? or ! each time. It is intended for values that are nil briefly at initialization but are guaranteed to hold a value before first use.
This repository is a concise, runnable Xcode playground that demonstrates the concept β and shows the key caveat: accessing an implicitly unwrapped optional while it is still nil prints nil (and would crash if it were force-unwrapped). It is meant as a small learning example, not a library.
- Declaring an implicitly unwrapped optional with the
Type!syntax. - Assigning
nilto an implicitly unwrapped optional. - Printing / accessing the value and observing the resulting output.
- The risk of accessing an implicitly unwrapped optional that is currently
nil.
git clone https://github.com/ahmetbostanciklioglu/ImplicitUnwrapping.git
cd ImplicitUnwrapping
unzip ImplicitUnwraping.playground.zipOpen ImplicitUnwraping.playground in Xcode and run it to see the results in the playground sidebar.
- Xcode 12 or later
- Swift 5
- iOS playground target
Ahmet BostancΔ±klΔ±oΔlu β @ahmetbostanciklioglu Β· ahmetbostancikli@gmail.com
β If this helped you, consider giving the repo a star!