Skip to content

Commit 8420dda

Browse files
committed
Frame force unwrapping around unrecoverable errors.
1 parent 7a4a53f commit 8420dda

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

TSPL.docc/LanguageGuide/TheBasics.md

+32-17
Original file line numberDiff line numberDiff line change
@@ -1494,19 +1494,17 @@ if let firstNumber = Int("4") {
14941494
using the && operator instead of a comma.
14951495
-->
14961496

1497-
<!-- XXX show an example of guard-let -->
1498-
14991497
Constants and variables created with optional binding in an `if` statement
15001498
are available only within the body of the `if` statement.
15011499
In contrast, the constants and variables created with a `guard` statement
15021500
are available in the lines of code that follow the `guard` statement,
15031501
as described in <doc:ControlFlow#Early-Exit>.
15041502

1503+
<!-- XXX guard-let is useful when the rest of a function or method accesses the unwrapped value -->
1504+
15051505
### Providing a Fallback Value
15061506

1507-
Instead of using `if`-`let` or `guard`-`let`
1508-
to skip a block of code when a value is `nil`,
1509-
another way to handle the missing value is to supply
1507+
Another way to handle a missing value is to supply
15101508
a default value using the “fallback” operator (`??`).
15111509
If the optional on the left side of the `??` isn't `nil`,
15121510
that value is unwrapped and used.
@@ -1592,19 +1590,36 @@ if convertedNumber != nil {
15921590
```
15931591
-->
15941592

1595-
Trying to use `!` to access a nonexistent optional value
1596-
triggers a runtime error.
1597-
Always make sure that an optional contains a non-``nil`` value
1598-
before using `!` to force-unwrap its value.
1593+
When you force unwrap a non-`nil` value,
1594+
the result is its unwrapped value.
1595+
Force unwrapping a `nil` value triggers a runtime error.
15991596

1600-
<!-- XXX
1601-
writing foo! means that you know foo won't be nil,
1602-
and you can convince another person,
1603-
but you can't encode that proof into the type system
1604-
in a way the compiler understands.
1605-
It's also used when a nil value indicates an unrecoverable (or programmer) error,
1606-
like failing to load a resource from within the app bundle.
1607-
-->
1597+
Because a `nil` value stops the program,
1598+
another reason to force unwrap an optional
1599+
is when `nil` represents an unrecoverable failure,
1600+
such a programmer error or corrupted data.
1601+
In that usage, the `!` is a shorter spelling of [`fatalError(_:file:line:)`][].
1602+
For example, the code below shows two equivalent approaches:
1603+
1604+
[`fatalError(_:file:line:)`]: https://developer.apple.com/documentation/swift/fatalerror(_:file:line:)
1605+
1606+
```swift
1607+
let number = convertedNumber!
1608+
1609+
guard let number = convertedNumber else {
1610+
fatalError("The number was invalid")
1611+
}
1612+
```
1613+
1614+
Both versions of the code above depend on `convertedNumber`
1615+
always containing a value.
1616+
Writing that requirement as part of the code,
1617+
using either of the approaches above,
1618+
lets your code check that the requirement is true at run time.
1619+
1620+
For more information about enforcing data requirements
1621+
and checking assumptions at runtime,
1622+
see <doc:TheBasics#Assertions-and-Preconditions> below.
16081623

16091624
### Implicitly Unwrapped Optionals
16101625

0 commit comments

Comments
 (0)