Created
July 14, 2020 07:12
-
-
Save sidepelican/6ec16b523679dce39baa929e559ee834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A { | |
class var className: String { "A" } | |
init() {} | |
func setup() { | |
name = { | |
return Self.className | |
} | |
} | |
var name: (() -> String)? | |
} | |
class B: A { | |
class override var className: String { "B" } | |
} | |
class TestWeak { | |
weak var object: A? | |
init(_ object: A) { | |
self.object = object | |
object.setup() | |
} | |
func main() { | |
print(object?.name!()) | |
} | |
} | |
class TestStrong { | |
var object: A? | |
init(_ object: A) { | |
self.object = object | |
object.setup() | |
} | |
func main() { | |
print(object?.name!()) | |
} | |
} | |
TestWeak(A()).main() // nil | |
TestWeak(B()).main() // nil | |
TestStrong(A()).main() // Optional("A") | |
TestStrong(B()).main() // Optional("B") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Self.className
はselfのクラスを元に分岐するので循環参照が起こるのかと思ったけど、そうでもないもちろん、
みたいなことを書いてselfをキャプチャすると循環参照する。