Skip to content

Instantly share code, notes, and snippets.

@timonus
Created March 24, 2023 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timonus/624bf04c9b596604c82ee03c98250f7c to your computer and use it in GitHub Desktop.
Save timonus/624bf04c9b596604c82ee03c98250f7c to your computer and use it in GitHub Desktop.
Swift Leak Playground
//
// ViewController.swift
// Playground
//
// Created by Tim Johnsen on 3/24/23.
//
import UIKit
class MyObject {
var block: ((UIButton) -> Void)? = nil
var button: UIButton? = nil
func setup() {
button = UIButton()
block = { /*[weak self]*/ button in // Try toggling this on and off and looking for the "DEALLOCATING" log line
button.addAction(UIAction { [weak self] _ in
self?.doSomething()
}, for: .touchUpInside)
}
}
func doSomething() {
}
deinit {
print("DEALLOCATING")
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let obj = MyObject()
obj.setup()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment