Reputation: 45
How can I delete an instance of a class in Swift? Looking for some kind of equivalent to
del instance
in Python. I'm pretty new to Swift, and I couldn't find anything in the documentation or any prior posts.
I have a bullet class in a game that needs to be deleted after colliding with a player. Example code could go something like
class Bullet() {
init() {
for traveled in 0...range {
//travel forward
if collided {
//damage collided player
del self
}
}
Edit: This is what cleared it up for me http://www.apeth.com/iOSBook/ch12.html#_memory_management
Upvotes: 2
Views: 10156
Reputation: 30431
Swift, as mentioned in another question, uses Automatic Reference Counting.
Apple explains it like this:
ARC tracks how many properties, constants, and variables are currently referring to each class instance. ARC will not deallocate an instance as long as at least one active reference to that instance still exists.
Here’s an example of how Automatic Reference Counting works. This example starts with a simple class called Person
, which defines a stored constant property called name
:
class Person {
let name: String
init(name: String) {
self.name = name
print("\(name) is being initialized")
}
deinit {
print("\(name) is being deinitialized")
}
}
The Person
class has an initializer that sets the instance’s name
property and prints a message to indicate that initialization is underway. The Person
class also has a deinitializer that prints a message when an instance of the class is deallocated.
The next code snippet defines three variables of type Person?
, which are used to set up multiple references to a new Person
instance in subsequent code snippets. Because these variables are of an optional type (Person?
, not Person
), they are automatically initialized with a value of nil
, and do not currently reference a Person
instance.
var reference1: Person?
var reference2: Person?
var reference3: Person?
You can now create a new Person
instance and assign it to one of these three variables:
reference1 = Person(name: "John Appleseed")
// Prints "John Appleseed is being initialized"
Note that the message "John Appleseed is being initialized"
is printed at the point that you call the Person
class’s initializer. This confirms that initialization has taken place.
Because the new Person
instance has been assigned to the reference1
variable, there is now a strong reference from reference1
to the new Person
instance. Because there is at least one strong reference, ARC makes sure that this Person
is kept in memory and is not deallocated.
If you assign the same Person
instance to two more variables, two more strong references to that instance are established:
reference2 = reference1
reference3 = reference1
There are now three strong references to this single Person
instance.
If you break two of these strong references (including the original reference) by assigning nil
to two of the variables, a single strong reference remains, and the Person
instance is not deallocated:
reference1 = nil
reference2 = nil
ARC does not deallocate the Person
instance until the third and final strong reference is broken, at which point it’s clear that you are no longer using the Person
instance:
reference3 = nil
// Prints "John Appleseed is being deinitialized"
All code and writing for ARC in Action
is from Apple's documentation.
Upvotes: 5
Reputation: 131471
Your question doesn't make a lot of sense in Swift. Swift uses ARC (Automatic Reference Counting) to manage object lifecycles.
As long as you maintain at least one strong reference to an object, it lives. As soon as there are no more strong references, it gets deallocated.
Consider the following code.
class FooClass {
var value: Int
}
var foo: FooClass?
foo = FooClass(value: 3)
//foo will be valid
foo = nil; //This causes the object in `foo` to be deallocated immediately.
The closest thing to del (assuming I understand what it does - I don't know Python) seems like assigning nil to an optional var, assuming there are no other strong references to the object.
Upvotes: 3