Last active
October 24, 2018 11:36
-
-
Save kasimte/c51258cb857d0e6c41c39b035e01511a to your computer and use it in GitHub Desktop.
An example of conditional String optional unwrapping in Swift 3.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var optional: String? = "Hello World" | |
| if let o = optional { // Same as: optional as String! | |
| print(o) | |
| } | |
| // Output: Hello World. | |
| if let o = optional as String! { | |
| print(o) | |
| } | |
| // Output: Hello World. | |
| var blankOptional: String? | |
| if let o = blankOptional { | |
| print(o) | |
| } | |
| // No output. | |
| if let o = optional as String { | |
| print(o) | |
| } | |
| // Error: String? is not convertible to String without force downcast. | |
| if let o = optional as? String { | |
| print(o) | |
| } | |
| // Warning: Conditional downcast from String? to String does nothing. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment