Skip to content

Instantly share code, notes, and snippets.

@tornikegomareli
Created July 9, 2023 18:56
Show Gist options
  • Save tornikegomareli/871f7cdadf57094f0e99aeaa54e12d5a to your computer and use it in GitHub Desktop.
Save tornikegomareli/871f7cdadf57094f0e99aeaa54e12d5a to your computer and use it in GitHub Desktop.
Weak Self Example
//
// 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