class Ball {
var size: Int
init(size: Int) {
self.size = size
}
}
class A {
var closure: () -> Void
init(closure: @escaping () -> Void) {
self.closure = closure
}
}
var rubberBall = Ball(size: 10)
var weakCaptureObj = A { [weak rubberBall] in
print("weakCaptureObj \t\t| rubberBall?.size: \(rubberBall?.size)")
}
var strongCaptureObj = A { [rubberBall] in
print("strongCaptureObj \t| rubberBall.size: \(rubberBall.size)")
}
var noCaptureObj = A {
print("noCaptureObj \t\t\t| rubberBall.size: \(rubberBall.size)")
}
print("before changing rubberBall size...")
weakCaptureObj.closure()
strongCaptureObj.closure()
noCaptureObj.closure()
print("")
rubberBall.size = 20
print("//rubberBall.size = 20//")
print("")
print("after changing rubberBall size...")
weakCaptureObj.closure()
strongCaptureObj.closure()
noCaptureObj.closure()
print("")
rubberBall = Ball(size: 100)
print("//rubberBall = Ball(size: 100)//")
print("")
print("after replacing rubberBall...")
weakCaptureObj.closure()
strongCaptureObj.closure()
noCaptureObj.closure()
print("")
strongCaptureObj = A { [rubberBall] in
print("strongCaptureObj \t| rubberBall.size: \(rubberBall.size)")
}
print(#"""
strongCaptureObj = A { [rubberBall] in
print("strongCaptureObj \t| rubberBall.size: \(rubberBall.size)")
}
"""#)
print("")
print("after replacing strongCaptureObj...")
weakCaptureObj.closure()
strongCaptureObj.closure()
noCaptureObj.closure()
print("")