How do optionals work in Swift.
Utilisateur anonyme
Optionals are exactly that, optional. If a variable is declared optional(?), you are stating that it might be initialized with a value or it might be nil. This allows further code in the block it's used to act as though it doesn't exist to prevent crashes at runtime if it's nil. However, in order to access it, you have to "unwrap" it using either a let or guard, or declaring it with a bash(!). Declaring an optional with bash means that you are guaranteeing that it will be initialized with a value. If you attempt to access this value, and it's not initialized, it will crash the application similar to a NullPointerExemption but this allows you to debug faster. In general, UI elements that may or may not appear on the screen (Like a UIAlert or UILabel that appears based on state change) should all be declared optional(?).