Skip to content

Instantly share code, notes, and snippets.

@jkereako
Created September 28, 2017 02:15
Show Gist options
  • Save jkereako/cf7a00c3e0ba71b604a8adb172f18fcf to your computer and use it in GitHub Desktop.
Save jkereako/cf7a00c3e0ba71b604a8adb172f18fcf to your computer and use it in GitHub Desktop.
Free `self` in Swift
final class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Copy the reference `self` into a variable.
var selfCopy = self
// Implicitly cast `selfCopy` to a UnsafeMutableRawPointer.
let ptr = UnsafeMutableRawPointer(&selfCopy)
// Set it free!
free(ptr)
// Here's another way...
withUnsafeMutablePointer(to: &selfCopy) { ptr in
free(ptr)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment