Skip to content

Instantly share code, notes, and snippets.

@dougboutwell
Last active September 3, 2025 18:58
Show Gist options
  • Select an option

  • Save dougboutwell/194a05fdf03751132d8dc16099bedc4a to your computer and use it in GitHub Desktop.

Select an option

Save dougboutwell/194a05fdf03751132d8dc16099bedc4a to your computer and use it in GitHub Desktop.
Retain cycles
class ViewManager {
var completion: (() -> Void)?
func setup(completion: @escaping () -> Void) {
self.completion = completion
}
}
class ViewController: UIViewController {
let nameLabel = UILabel()
var manager = ViewManager()
override func viewDidLoad() {
super.viewDidLoad()
/* Setup nameLabel, etc - assume it's done sensibly */
}
override func viewWillAppear() {
self.function1()
}
func function1() {
manager.setup {
self.nameLabel.frame.origin.x += 100
}
}
func function2() {
manager.setup { [weak self] in
self?.nameLabel.frame.origin.x += 100
}
}
func function3() {
manager.setup { [unowned self] in
self.nameLabel.frame.origin.x += 100
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment