Skip to content

Commit

Permalink
Correct the idiom for extending lifetime.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcritz committed Aug 3, 2018
1 parent 4d4a01e commit a20abff
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.markdown
Expand Up @@ -682,16 +682,16 @@ Code (even non-production, tutorial demo code) should not create reference cycle

### Extending object lifetime

Extend object lifetime using the `[weak self]` and `guard let strongSelf = self else { return }` idiom. `[weak self]` is preferred to `[unowned self]` where it is not immediately obvious that `self` outlives the closure. Explicitly extending lifetime is preferred to optional unwrapping.
Extend object lifetime using the `[weak self]` and ```guard let `self` = self else { return }``` idiom. `[weak self]` is preferred to `[unowned self]` where it is not immediately obvious that `self` outlives the closure. Explicitly extending lifetime is preferred to optional unwrapping. In Swift 4.2, you should drop the backticks in the guard statement.

**Preferred**
```swift
resource.request().onComplete { [weak self] response in
guard let strongSelf = self else {
guard let `self` = self else {
return
}
let model = strongSelf.updateModel(response)
strongSelf.updateUI(model)
let model = self.updateModel(response)
self.updateUI(model)
}
```

Expand Down

1 comment on commit a20abff

@XunlaY
Copy link

@XunlaY XunlaY commented on a20abff Sep 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The let 'self' = self statement is a compiler bug and should be avoided.
Maybe you should point out 2 variants of this.
Version < 4.2: guard let strongSelf = self

Version > 4.2: guard let self = self

Please sign in to comment.