Merge pull request #22 from dennda/no-force-unwrapping
Propose not using force-unwrapping of optionals.
This commit is contained in:
Коммит
d262cdd736
23
README.md
23
README.md
|
@ -34,6 +34,29 @@ It becomes easier to reason about code. Had you used `var` while still making th
|
|||
|
||||
Accordingly, whenever you see a `var` identifier being used, assume that it will change and ask yourself why.
|
||||
|
||||
#### Avoid Using Force-Unwrapping of Optionals
|
||||
|
||||
If you have an identifier `foo` of type `FooType?` or `FooType!`, don't force-unwrap it to get to the underlying value (`foo!`) if possible.
|
||||
|
||||
Instead, prefer this:
|
||||
|
||||
```swift
|
||||
if let foo = foo {
|
||||
// Use unwrapped `foo` value in here
|
||||
} else {
|
||||
// If appropriate, handle the case where the optional is nil
|
||||
}
|
||||
```
|
||||
|
||||
Alternatively, you might want to use Swift's Optional Chaining in some of these cases, such as:
|
||||
|
||||
```swift
|
||||
// Call the function if `foo` is not nil. If `foo` is nil, ignore we ever tried to make the call
|
||||
foo?.callSomethingIfFooIsNotNil()
|
||||
```
|
||||
|
||||
_Rationale:_ Explicit `if let`-binding of optionals results in safer code. Force unwrapping is more prone to lead to runtime crashes.
|
||||
|
||||
#### Avoid Using Implicitly Unwrapped Optionals
|
||||
|
||||
Where possible, use `let foo: FooType?` instead of `let foo: FooType!` if foo may be nil (Note that in general, `?` can be used instead of `!`).
|
||||
|
|
Загрузка…
Ссылка в новой задаче