Questions tagged [deinit]

Swift programming language object destructor

A deinitializer is called immediately before a class instance is deallocated. You write deinitializers with the deinit keyword, similar to how initializers are written with the init keyword. Deinitializers are only available on class types.

102 questions
2
votes
1 answer

swift deinit method not calling

In UIViewController deinit method not calling in ios swift4.2 I have tried below code for navigate to next viewController after navigation in popToViewcontroller deinit method not calling. let data = isSearchEnabled ?…
Jagadeesh K
  • 121
  • 2
  • 9
2
votes
2 answers

How do I deinitialize a child UIViewController?

I have several UIViewControllers which are added to a content view. After calling my remove function, I'm noticing that the child UIViewController's deinit function is not being called unless I explicitly set the value of the child UIViewController…
2
votes
1 answer

Defer statement at the end of deinit produces a warning

Since the Xcode 10.2 (Swift 5) the defer statement at the end of the deinit scope produces: 'defer' statement before end of scope always executes immediately; replace with 'do' statement to silence this warning Let's take a look at this…
Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
2
votes
3 answers

Deinit is it a good practice to implement it on viewControllers?

I am wondering if it is a good practice to implement a deinit on every view controller to check if it is correctly removed when it disappears and avoiding leaking memory?
Arrabidas92
  • 1,113
  • 1
  • 9
  • 20
2
votes
1 answer

Deinit UITabBarController after presenting new ViewController

I have an app in which users can log in. If the user launches the app and is already logged in then the rootViewController is set to my custom UITabBarController but when the user is not logged in the rootViewController is set to LoginVC (View…
Phyber
  • 1,368
  • 11
  • 25
2
votes
0 answers

Delete variable securely in Swift?

I have a Login screen where a user has to enter its username and password. I always store the password in a variable and send it over to the server. Then I leave the functions context and everything is fine because I have no reference to the…
FlixMa
  • 944
  • 1
  • 7
  • 20
2
votes
1 answer

Lazy initialization and deinit

I would to know if it's possible, in my view controller, use a lazy property and in deinit method call a method of my lazy property only if it was initialized. Below some code: fileprivate lazy var session: MySession = { let session: MySession =…
Giorgio
  • 1,973
  • 4
  • 36
  • 51
2
votes
2 answers

OSX deinit is not being called after dismissing view controller

There were topics when breakpoint was not hit in the "deinit" method. The solution was to put an executable code inside. Tried that - didn't work. The code to initiate a ViewController from the first window: let vc =…
Ruzard
  • 1,167
  • 3
  • 16
  • 33
2
votes
1 answer

How to declare weak notification

Maybe the title does not provide a good description so please read the following. I have a notification that is set to listen to an event: NSNotificationCenter.defaultCenter().addObserver(self, selector:…
Tung Fam
  • 7,899
  • 4
  • 56
  • 63
2
votes
0 answers

UIButton deinit not being called

I have a custom UIButton and want to remove it after I've pressed it. But somehow deinit isn't called when I try so. I've tried this in an empty project: class CustomButton: UIButton { deinit { print("deinit") } } class ViewController:…
Henny Lee
  • 2,970
  • 3
  • 20
  • 37
2
votes
1 answer

deinit in child view controllers

I have a tab bar controller and one of the tabs consists of a table view. When clicking on a row I segue to a new child controller. In this new controller, I have a deinit which removes an observer and prints "controller is de-initialised here". I…
user2363025
  • 6,365
  • 19
  • 48
  • 89
2
votes
1 answer

How to properly clean up an NSURLSession in Swift

I am creating an NSURLSession on init of a class with the following code: dispatch_once(&Static.token) { [unowned self] in let configuration =…
QuinnBaetz
  • 559
  • 9
  • 23
2
votes
1 answer

Swift Method: deinit is not invoked

Here is a code example from Swift Programming Book by Apple (in chapter: Deinitializers) struct Bank { static var coinsInBank = 10_000 static func vendCoins(coins: Int)-> Int { var coinsToVend = min(coins, coinsInBank) …
Tushar
  • 3,022
  • 2
  • 26
  • 26
1
vote
0 answers

Remove UIViewController from UIView and release memory

I'm adding UIViewController in another UIView using: self.addChild(vc) self.view.addSubview(vc.view) vc.didMove(toParent: self) and removing that UIViewController by: for childVC in self.children { childVC.willMove(toParent: nil) …
Deep Gandhi
  • 157
  • 1
  • 4
  • 13
1
vote
2 answers

When is `deinit` exactly called? (in Swift)

When is deinit exactly called? Is it like C++ guaranteed to be called when last reference gets out of scope (by return, throw or exit)? Or is Swift using Garbage-Collector?
Top-Master
  • 7,611
  • 5
  • 39
  • 71