Swift의 weak self capturing: 다른 방식과의 비교

Wonil Lee·2024년 2월 13일
0
class ClosureGiver {
    
    var closureToGive: () -> Void
    
    init(closureToGive: @escaping () -> Void) {
        self.closureToGive = closureToGive
    }
    
    func passWithWeakSelf(holder: ClosureHolder) -> Void {
        holder.holdingClosure = { [weak self] in
            if let self {
                self.closureToGive()
            } else {
                print("  (self is lost)")
            }
        }
    }
    
    func passWithStrongClosure(holder: ClosureHolder) -> Void {
        holder.holdingClosure = { [closure = self.closureToGive] in
            closure()
        }
    }
    
    func passPlain(holder: ClosureHolder) -> Void {
        holder.holdingClosure = {
            self.closureToGive()
        }
    }
}

class ClosureHolder {
    
    var holdingClosure: () -> Void = {}
}

var closureGiver = ClosureGiver(closureToGive: { print("  initial closure runs") })
var weakSelfClosureHolder = ClosureHolder()
var strongClosureClosureHolder = ClosureHolder()
var plainHolder = ClosureHolder()

closureGiver.passWithWeakSelf(holder: weakSelfClosureHolder)
closureGiver.passWithStrongClosure(holder: strongClosureClosureHolder)
closureGiver.passPlain(holder: plainHolder)

print("before modifying closureToGive")
print("weakSelfClosureHolder.holdingClosure():")
weakSelfClosureHolder.holdingClosure()
print("plainHolder.holdingClosure():")
plainHolder.holdingClosure()
print("strongClosureClosureHolder.holdingClosure():")
strongClosureClosureHolder.holdingClosure()
print("")

closureGiver.closureToGive = { print("  new closure runs") }

print(
#"""
//
closureGiver.closureToGive = { print("  new closure runs") }
//
"""#
)

print("after modifying closureToGive")
print("weakSelfClosureHolder.holdingClosure():")
weakSelfClosureHolder.holdingClosure()
print("plainHolder.holdingClosure():")
plainHolder.holdingClosure()
print("strongClosureClosureHolder.holdingClosure():")
strongClosureClosureHolder.holdingClosure()
print("")

closureGiver = ClosureGiver(closureToGive: { print("  new giver's closure runs") })

print(
#"""
//
closureGiver = ClosureGiver(closureToGive: { print("  new giver's closure runs") })
// initial closureGiver's reference count is now 1
//
"""#
)

print("after replacing closureGiver variable")
print("weakSelfClosureHolder.holdingClosure():")
weakSelfClosureHolder.holdingClosure()
print("plainHolder.holdingClosure():")
plainHolder.holdingClosure()
print("strongClosureClosureHolder.holdingClosure():")
strongClosureClosureHolder.holdingClosure()
print("")

plainHolder = ClosureHolder()
plainHolder.holdingClosure = { print("  new plainHolder's closure runs") }

print(
#"""
//
plainHolder = ClosureHolder()
plainHolder.holdingClosure = { print("  new plainHolder's closure runs") }
// initial closureGiver's reference count is now 0
//
"""#
)

print("after replacing plainHolder")
print("weakSelfClosureHolder.holdingClosure():")
weakSelfClosureHolder.holdingClosure()
print("plainHolder.holdingClosure():")
plainHolder.holdingClosure()
print("strongClosureClosureHolder.holdingClosure():")
strongClosureClosureHolder.holdingClosure()
print("")

/*결과
before modifying closureToGive
weakSelfClosureHolder.holdingClosure():
  initial closure runs
plainHolder.holdingClosure():
  initial closure runs
strongClosureClosureHolder.holdingClosure():
  initial closure runs

//
closureGiver.closureToGive = { print("  new closure runs") }
//
after modifying closureToGive
weakSelfClosureHolder.holdingClosure():
  new closure runs
plainHolder.holdingClosure():
  new closure runs
strongClosureClosureHolder.holdingClosure():
  initial closure runs

//
closureGiver = ClosureGiver(closureToGive: { print("  new giver's closure runs") })
// initial closureGiver's reference count is now 1
//
after replacing closureGiver variable
weakSelfClosureHolder.holdingClosure():
  new closure runs
plainHolder.holdingClosure():
  new closure runs
strongClosureClosureHolder.holdingClosure():
  initial closure runs

//
plainHolder = ClosureHolder()
plainHolder.holdingClosure = { print("  new plainHolder's closure runs") }
// initial closureGiver's reference count is now 0
//
after replacing plainHolder
weakSelfClosureHolder.holdingClosure():
  (self is lost)
plainHolder.holdingClosure():
  new plainHolder's closure runs
strongClosureClosureHolder.holdingClosure():
  initial closure runs
*/

profile
DEV WITH APPLE

0개의 댓글