@@ -1494,19 +1494,17 @@ if let firstNumber = Int("4") {
1494
1494
using the && operator instead of a comma.
1495
1495
-->
1496
1496
1497
- <!-- XXX show an example of guard-let -->
1498
-
1499
1497
Constants and variables created with optional binding in an ` if ` statement
1500
1498
are available only within the body of the ` if ` statement.
1501
1499
In contrast, the constants and variables created with a ` guard ` statement
1502
1500
are available in the lines of code that follow the ` guard ` statement,
1503
1501
as described in < doc:ControlFlow#Early-Exit > .
1504
1502
1503
+ <!-- XXX guard-let is useful when the rest of a function or method accesses the unwrapped value -->
1504
+
1505
1505
### Providing a Fallback Value
1506
1506
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
1510
1508
a default value using the “fallback” operator (` ?? ` ).
1511
1509
If the optional on the left side of the ` ?? ` isn't ` nil ` ,
1512
1510
that value is unwrapped and used.
@@ -1592,19 +1590,36 @@ if convertedNumber != nil {
1592
1590
```
1593
1591
-->
1594
1592
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.
1599
1596
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.
1608
1623
1609
1624
### Implicitly Unwrapped Optionals
1610
1625
0 commit comments