Created
July 9, 2023 18:56
-
-
Save tornikegomareli/871f7cdadf57094f0e99aeaa54e12d5a to your computer and use it in GitHub Desktop.
Weak Self Example
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
// | |
// main.swift | |
// Closures-4Jul2023 | |
// | |
// Created by Tornike on 04/07/2023. | |
// | |
import Foundation | |
typealias OnAuthenticateCompletion = ((Bool) -> Void) | |
var closureVariable: OnAuthenticateCompletion? | |
class Manager { | |
var managerName: String | |
public init(name: String) { | |
managerName = name | |
} | |
func bind() { | |
var number = 10 | |
closureVariable = { [weak self] isSuccess in | |
guard let self = self else { | |
print(" Manager object is dead, and closure is not possible to happen") | |
return | |
} | |
if isSuccess { | |
print(self.managerName) | |
print(number) | |
} else { | |
print("KARGAD IKAVI") | |
} | |
} | |
print("Bind function finished") | |
} | |
} | |
var manager: Manager? = Manager(name: "Manager") //1 | |
manager?.bind() // 1 | |
manager = nil // 0 | |
closureVariable?(true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment