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
65
votes
13 answers

Deinit never called

I'm creating a ViewController object an pushing it to a navigation controller. When the object is being popped from the stack - it is not being release and Deinit is not being called. What can be the reason for that? Here's the code that…
YogevSitton
  • 10,068
  • 11
  • 62
  • 95
15
votes
5 answers

Deallocate SKScene after transition to another SKScene in SpriteKit

I have a view controller that has three skscenes as children. When I transition from one to another, the old skscene doesn't get deallocated. I want it to get deallocated as if it was never there. Example: When I first load the app, only 1 skscene…
Max Hudson
  • 9,961
  • 14
  • 57
  • 107
12
votes
1 answer

UITableViewCell object deinit never called

the problem is in my project. But I also tried in new xcode draft project that Master-Detail Application one. I have created base Cell class and mapped the cell on storyboard. After delete operation deinit never called. There is no strong or weak…
ervanux
  • 281
  • 3
  • 9
11
votes
2 answers

Can I use didSet in deinit?

I added a variable of Timer to my class, and used its didSet observer to invalidate old value var timer: Timer? { didSet { oldValue?.invalidate() } } deinit { timer = nil } I thought this would be enough to invalidate timer when class is…
Damian Dudycz
  • 2,622
  • 19
  • 38
11
votes
1 answer

Swift's deinit is not called

private let DBItemCellIdentifier = "ItemCellIdentifier" private let DBItemSegueIdentifier = "ItemSegueIdentifier" class DBItemsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, DBItemTableViewCellDelegate { …
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
8
votes
3 answers

iOS unable to remove Notification observer. Deinit not getting called

I have a UIView similar to the one you can see below: class ViewTaskViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { override func viewDidLoad() { super.viewDidLoad() subscribeToNotifications() } func…
Sohil Pandya
  • 755
  • 1
  • 7
  • 19
6
votes
1 answer

Invoke code in extension on object deinit?

In Swift (I'm using 4.1), is there a way to do some clean-up in an extension when an object is being destructed? I have in mind the kind of code that would go in deinit(), but an extension can't declare deinit(). (Besides which, if several…
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
6
votes
0 answers

UISplitViewController: Deinit DetailView in collapsed mode

I've been struggling on this for a while now, but I wasn't able to find a solution: I've got an iOS 9 app that supports all device families, uses size classes and is programmed with Swift 2.0. I'm using a UISplitViewController and everything works…
croX
  • 1,725
  • 1
  • 19
  • 23
5
votes
1 answer

Is it possible to override deinit in Swift?

When creating a subclass from another class, it is required to override the init() function, but you cannot override the deinit 'function'. Is this possible in Swift? Here is an example class Foo { init(){ print("Foo created") } …
Danoram
  • 8,132
  • 12
  • 51
  • 71
5
votes
2 answers

how to call deinit method within class definition in swift

I want to define a method that can destroy the instance it belongs to when a variable in this class has increased to a certain value. I attempted to do something like following: var calledTimes = 0 //some other method would update this value func…
Joseph Zhou
  • 125
  • 1
  • 2
  • 12
4
votes
1 answer

NotificationCenter.default.removeObserver(self) in deinit, why deinit it even get called?

Starting with ios 9 it is not necessary to unsubscribe from notification center since ios handles this automatically, but prior to ios 9 developers had to manually call NotificationCenter.default.removeObserver(self) in order to avoid memory leaks…
starwarrior8809
  • 361
  • 1
  • 10
4
votes
2 answers

Swift iOS -Should Deinit get called inside child View Controller when added as a child to another View Controller?

I have a childVC(vc3) inside a parentVC(vc2) inside another parentVC(vc1). I'm doing it this way for animation purposes. What happens is I add vc3 as a child to vc2. I have a collectionView that pushes on vc1. Once vc1 is on scene vc2 is added to…
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
4
votes
2 answers

Reference type inside value type

I am exploring Swift value types particularly structs to get a better understanding of it's uses in different scenario. I was amazed to see how enum can be used to build Binary Search Tree using indirect which introduces a thin layer of reference…
Rahul
  • 2,056
  • 1
  • 21
  • 37
4
votes
1 answer

Why is deinit not called until UIView is added to parent again?

I have a UIView that am adding to a UIViewController and am generally testing de-initialization to make sure I am doing things right. But when I don't set the variable in my viewController to nil and only use .removeFromSuperView() , the deinit()…
TheBen
  • 3,410
  • 3
  • 26
  • 51
4
votes
1 answer

swift deinit method not working when UIView is removed from memory

In Swift ARC is calling deinit when any UIViewController is removing from memory , but it's not getting called if any UIView is removed from memory. for example In case of UIViewController Class deinit is working great class…
Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63
1
2 3 4 5 6 7