27
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

guard let `self` = self ... を使おうとしているそこの貴方!ちょっとお待ちください!

Last updated at Posted at 2018-01-23

20181106追記

4.2でimplementedになったようです
https://github.com/apple/swift-evolution/blob/master/proposals/0079-upgrade-self-from-weak-to-strong.md

このような書き方ができるようになりました


{ [weak self] response in
  guard let self = self else {
    return
  } 
  self.reload(response)
}

↓は4.1以前の内容


weak self参照を↓のようにアンラップするパターンが多いかと思いますが


{ [weak self] response in
  guard let `self` = self else {
    return
  } 
  self.reload(response)
}

これができてしまうのはバグだそうです
https://github.com/apple/swift-evolution/blob/master/proposals/0079-upgrade-self-from-weak-to-strong.md

AppleのChris Lattnerは「これはコンパイラのバグです」と述べています。
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160118/007425.html

self を使ってしまうと、このバグが修正されて使えなくなったとき、どこを直せばいいのかわかりにくいので、 strongSelf, me, などなどわかりやすい別名を使ったほうが良さそうです

27
20
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?