0

This might be a crazy question, but its something that has been bugging me this week. In Swift if you have an optional instance of a class does setting that optional to nil call deinit (if implemented) or do any cleanup on the instance?

// OPTIONAL INSTANCE OF PLANET
var newPlanet: Planet? = Planet(name: "Earth", atmosphere: "Oxygen")
if let planet = newPlanet {
    planet.description()
}
newPlanet = nil

I am assuming that after setting the pointer to nil that particular instance of Planet is no longer accessible?

fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
  • 1
    Have you tried it? Just implement `deinit` and add a `println`. – Martin R Oct 22 '14 at 16:19
  • I did, and it does not get called, well not in playgrounds anyways. I was just wondering what happens in this situation. – fuzzygoat Oct 22 '14 at 16:24
  • 1
    Releasing objects in the Playground does not work reliably. I think that is documented in some release notes. It should work as expected in a compiled project. – Instead of asking if deinit is called or not, it would be more constructive to tell your actual experience, and add the information about the Playground. – Martin R Oct 22 '14 at 16:26
  • Ah I see, much appreciated. To be fair I neglected to mention the Playground as I did not think it was related, now I see it is, I concede it would have been useful to note. – fuzzygoat Oct 22 '14 at 16:56

0 Answers0