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
4
votes
2 answers

Does Cocoa connection binding to NSToolbarItem prevent deinitializing?

Trying to set the selected segment of an NSToolbarItem that's an NSSegmentedControl via connection binding to a property (optionSegment). Subclassing the window controller as such class MyWindow: NSWindowController { dynamic var optionSegment:…
JKaz
  • 765
  • 6
  • 18
4
votes
2 answers

Zeroing weak variable and deinit, which one happens first?

Which one happens first? Zeroing (nilling) weak variable. deinit
eonil
  • 83,476
  • 81
  • 317
  • 516
3
votes
1 answer

How to clean up environment object after Swiftui view is dismissed?

I have a fairly large project that I started with storyboards and xibs. Now I'm implementing some new views using swiftui. This is the code I'm using to do that: func goToSchedule() { let scheduleController = ScheduleController() let vc =…
Watermamal
  • 357
  • 3
  • 12
3
votes
1 answer

How ARC act with force unwrapped instance

I'mm trying to learn how ARC works exactly, so I read Swift ARC documentation and I followed the example that they provide in the document using playground: class Person { let name: String weak var apartment: Apartment? init(name:…
A.Munzer
  • 1,890
  • 1
  • 16
  • 27
3
votes
2 answers

deinit not called on boilerplate macOS app

I've created new app from macOS Cocoa App template. The only change I've made is added deinit in NSViewController. So now it looks like this(complete code): import Cocoa class ViewController: NSViewController { override func viewDidLoad() { …
Pablo
  • 28,133
  • 34
  • 125
  • 215
3
votes
2 answers

Why is deinit not called on UITabBarController?

I'm in the process of learning Swift. I wouldn't say I'm a novice, but I'm sure like many learning online I have missed a lot of fundamental steps to understanding what's really going on. After getting pretty far with my app I am now seeing that my…
Dan
  • 543
  • 2
  • 13
3
votes
2 answers

Is it possible to swizzle deinit using swift. if yes then how to achieve this

I want to log some statements in deinit in each subclass of UIViewController in my project. I don't want to copy/paste the same lines in each view controller subclass.
ZaEeM ZaFaR
  • 1,508
  • 17
  • 22
3
votes
2 answers

how to make deinit take effect in swift

I have a Car class. Let's say a car goes to the junkyard, this car should no longer be counted in the total population. I have the deinit function, but how do I systematically remove a car from the car population? In other words, how do I get the…
Govind Rai
  • 14,406
  • 9
  • 72
  • 83
3
votes
2 answers

deinit in empty swift Project not called

If i create an new "Single View Application" project with xcode(6.3.2) and add only a deinit function to the ViewController. Run app, close app. Deinit is never called! I tested it with several devices and the simulator. deinit { …
loop masta
  • 149
  • 11
2
votes
1 answer

Should an Instance of a Swift Class be assigned to `nil` to see the deinitializer in action?

Swift can have Deinitializers (Like C++ Destructors) for Classes. When I am using a Non-Optional Instance of a Class (That is, var obj: Class not var obj: Class?), I am unable to see the message printed by the Deinitializer. However, when an…
Kris2k
  • 275
  • 2
  • 12
2
votes
1 answer

Testing for UINavigationController deallocation is failing?

I have the following unit test: func testReferences() throws { var strongVC: UIViewController? = UIViewController() var strongNC: UINavigationController? = UINavigationController(rootViewController: strongVC!) weak var weakVC…
Steve
  • 31
  • 2
2
votes
1 answer

How to deinit ViewModel with combine framework when views disappears

I use Combine in viewModels to update the views. But if I store the AnyCancellable objects into a set of AnyCancellable, the deinit method is never called. I use the deinit to cancel all cancellables objects. struct View1: View { …
Dokoeur
  • 23
  • 1
  • 4
2
votes
1 answer

Deinit method called automatically when its instance is initialised and delegate is set to some view controller

When I initialise the ABCViewController instance to push this controller, like I did it here. private func showABCController() { let storyBoard = UIStoryboard ( name: "PaymentView", bundle: Bundle(for: ABCViewController.self) …
Himan Dhawan
  • 894
  • 5
  • 23
2
votes
1 answer

RxSwift; ViewController never deinitialized

I have a ViewController (called MainViewController) which is backed by a ViewModel (called MainViewModel). The ViewModel has a variable that defines which child ViewController MainViewController should present as its child. My problem is that, when…
Wiingaard
  • 4,150
  • 4
  • 35
  • 67
2
votes
0 answers

How to deinit ViewControllers embedded in a TabBarController [Swift 5]

I have got two ViewControllers encapsulated and instantiated in a TabBarController. The TabBarController is instantiated in a superordinate NavigationController. When the NavigationController decides to go back and 'leave' the TabBarController and…
PascalS
  • 975
  • 1
  • 16
  • 40