I understand about how ARC decrease the reference count every time the strong reference variable is set to nil, or is out of scope. But in this scenario I confused about why ARC doesn't deallocate the object.
Please take a look at this simple code:
class Person {
var name: String
init(name: String) {
self.name = name
}
deinit {
print("deinit person named \(name)")
}
}
class Main {
func run() {
var personArray = [Person(name: "John"), Person(name: "Doe")]
// This will not deallocate the Person object
personArray.remove(at: 1)
personArray.remove(at: 0)
// This will deallocate the Person Object
// personArray.removeAll()
print("\nEnd of run method\n")
}
}
let main = Main()
main.run()
As you can see here, when I use removeAll()
the object is deallocated instantly. You can see the log below:
But when I use remove(at:)
the object doesn't get deallocated. As you can see in this log below:
This is just a simple example, but you can see that the ARC doesn't deallocate the object when using remove(at:)
. Why is that?