Skip to content

Instantly share code, notes, and snippets.

@TachibanaKaoru
Created December 11, 2018 21:20
Show Gist options
  • Save TachibanaKaoru/38db2a26403bfa5878e864c476e27d64 to your computer and use it in GitHub Desktop.
Save TachibanaKaoru/38db2a26403bfa5878e864c476e27d64 to your computer and use it in GitHub Desktop.
Circular reference (noescape)
class Person{
var name: String
init(name: String) {
print("--- init \(name) ---")
self.name = name
}
deinit {
print("--- deinit \(name) ---")
}
func normalHello(){
print("Hello, I am \(name)!")
}
// MARK: - Closure関連
private var keepingClosure:(()->Void)?
// これはescaping
private func keepAndDo(_ closure:@escaping (()-> Void)){
self.keepingClosure = closure
closure()
}
// これはnoescape(デフォルトは noescape)
private func justDo(_ closure: (()-> Void)){
closure()
}
// MARK: - Hello系
/// これはweakをいれていないので失敗しちゃうもの
func strongHello(){
self.keepAndDo {
self.normalHello()
}
}
func weakHello(){
self.keepAndDo { [weak self] in
self?.normalHello()
}
}
func noescapeHello(){
self.justDo {
self.normalHello()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment