Skip to content

Commit fe8169b

Browse files
committed
Comparison to nil is unrelated to force unwrapping.
Code like the following is an anti-pattern -- Swift has better idioms for working with optionals: if x != nil { doSomething(x!) } The only reason to compare == or != nil is for flow control -- so moved the information there.
1 parent 913949a commit fe8169b

File tree

1 file changed

+38
-60
lines changed

1 file changed

+38
-60
lines changed

TSPL.docc/LanguageGuide/TheBasics.md

Lines changed: 38 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,34 @@ var surveyAnswer: String?
12701270
```
12711271
-->
12721272

1273+
You can use an `if` statement to find out whether an optional contains a value
1274+
by comparing the optional against `nil`.
1275+
You perform this comparison with the “equal to” operator (`==`)
1276+
or the “not equal to” operator (`!=`).
1277+
1278+
If an optional has a value, it's considered to be “not equal to” `nil`:
1279+
1280+
```swift
1281+
let possibleNumber = "123"
1282+
let convertedNumber = Int(possibleNumber)
1283+
1284+
if convertedNumber != nil {
1285+
print("convertedNumber contains some integer value.")
1286+
}
1287+
// Prints "convertedNumber contains some integer value."
1288+
```
1289+
1290+
<!--
1291+
- test: `optionals`
1292+
1293+
```swifttest
1294+
-> if convertedNumber != nil {
1295+
print("convertedNumber contains some integer value.")
1296+
}
1297+
<- convertedNumber contains some integer value.
1298+
```
1299+
-->
1300+
12731301
You can't use `nil` with non-optional constants or variables.
12741302
If a constant or variable in your code needs to work with
12751303
the absence of a value under certain conditions,
@@ -1282,8 +1310,7 @@ you'll get a compile-time error.
12821310
This separation of optional and non-optional values
12831311
lets you explicitly mark what information can be missing,
12841312
and makes it easier to write correct code that handle missing values.
1285-
You can't accidentally treat an optional as if it were non-optional,
1286-
forgetting to check for `nil` and assuming there's a value,
1313+
You can't accidentally treat an optional as if it were non-optional
12871314
because this mistake produces an error at compile time.
12881315
After you unwrap the value,
12891316
none of the other code that works with that value needs to check for `nil`,
@@ -1533,73 +1560,24 @@ see <doc:BasicOperators#Nil-Coalescing-Operator>.
15331560

15341561
### Force Unwrapping
15351562

1536-
You can use an `if` statement to find out whether an optional contains a value
1537-
by comparing the optional against `nil`.
1538-
You perform this comparison with the “equal to” operator (`==`)
1539-
or the “not equal to” operator (`!=`).
1540-
1541-
If an optional has a value, it's considered to be “not equal to” `nil`:
1542-
1543-
```swift
1544-
let possibleNumber = "123"
1545-
let convertedNumber = Int(possibleNumber)
1546-
1547-
if convertedNumber != nil {
1548-
print("convertedNumber contains some integer value.")
1549-
}
1550-
// Prints "convertedNumber contains some integer value."
1551-
```
1552-
1553-
<!--
1554-
- test: `optionals`
1555-
1556-
```swifttest
1557-
-> if convertedNumber != nil {
1558-
print("convertedNumber contains some integer value.")
1559-
}
1560-
<- convertedNumber contains some integer value.
1561-
```
1562-
-->
1563-
1564-
After you're sure that the optional contains a value,
1565-
one way to access its underlying value
1566-
is adding an exclamation mark (`!`) to the end of the optional's name.
1567-
The exclamation point effectively says,
1568-
“I know that this optional definitely has a value; please use it.”
1569-
This is known as *forced unwrapping* of the optional's value:
1570-
1571-
```swift
1572-
if convertedNumber != nil {
1573-
print("convertedNumber has an integer value of \(convertedNumber!).")
1574-
}
1575-
// Prints "convertedNumber has an integer value of 123."
1576-
```
1577-
1578-
<!--
1579-
- test: `optionals`
1580-
1581-
```swifttest
1582-
-> if convertedNumber != nil {
1583-
print("convertedNumber has an integer value of \(convertedNumber!).")
1584-
}
1585-
<- convertedNumber has an integer value of 123.
1586-
```
1587-
-->
1588-
1563+
When `nil` represents an unrecoverable failure,
1564+
such a programmer error or corrupted state,
1565+
you can access the underlying value
1566+
by adding an exclamation mark (`!`) to the end of the optional's name.
1567+
This is known as *force unwrapping* the optional's value.
15891568
When you force unwrap a non-`nil` value,
15901569
the result is its unwrapped value.
15911570
Force unwrapping a `nil` value triggers a runtime error.
15921571

1593-
Because a `nil` value stops the program,
1594-
another reason to force unwrap an optional
1595-
is when `nil` represents an unrecoverable failure,
1596-
such a programmer error or corrupted data.
1597-
In that usage, the `!` is a shorter spelling of [`fatalError(_:file:line:)`][].
1572+
The `!` is, effectively, a shorter spelling of [`fatalError(_:file:line:)`][].
15981573
For example, the code below shows two equivalent approaches:
15991574

16001575
[`fatalError(_:file:line:)`]: https://developer.apple.com/documentation/swift/fatalerror(_:file:line:)
16011576

16021577
```swift
1578+
let possibleNumber = "123"
1579+
let convertedNumber = Int(possibleNumber)
1580+
16031581
let number = convertedNumber!
16041582

16051583
guard let number = convertedNumber else {

0 commit comments

Comments
 (0)