I'm a little confused regarding Swift's memory management. Can someone explain to me how come kid1 always stays at the same memory address? Even when I do kid1=kid2 or initialize a new object?
3 Answers
Your code prints the memory location of the kid1 variable,
and that does not change if you assign a new value to the variable.
If Kid is a reference type (class) then you can use 
ObjectIdentifier to get a unique identifier for the class instance
that the variable references: 
var kid1 = Kid(name: "A")
var kid2 = Kid(name: "B")
print(ObjectIdentifier(kid1)) // ObjectIdentifier(0x0000000100b06220)
print(ObjectIdentifier(kid2)) // ObjectIdentifier(0x0000000100b06250)
kid1 = kid2
print(ObjectIdentifier(kid1)) // ObjectIdentifier(0x0000000100b06250)
The object identifier happens to be the address of the pointed-to instance, but that is an undocumented implementation detail. If you need to convert an object reference to a real pointer then you can do (compare How to cast self to UnsafeMutablePointer<Void> type in swift)
print(Unmanaged.passUnretained(kid1).toOpaque())
Why kid1 is pointing to the same MemoryAddress each time?
In general, a class is a reference type. Which means, all instances of a class will share a single copy of data.
I.e, it's like a mutable data, if you change a data at any once instance of class, then it will affect that change to all its dependent instances.
It mainly deals with the memory addresses.
I think you have declared your class like below:
class Kid {
    var name: String?
    init(name:String) {
        self.name = name
    }
}
then for
- var kid1 = Kid(name: "A"): For- kid1instance it will assign some memory address, say- <Kid: 0x60400024b400>
- var kid2 = Kid(name: "B"): For- kid2instance it will assign some other memory address, say- <Kid: 0x60400024b760>
- when you do - kid1 =kid2:- kid1memory address will get changed to- kid2memory address. So,- kid1and- kid2will pointing to same memory address.
- kid1.name = "C": now if a change- kid1.name,..it will reflect to- kid2.namevalue also,because both are pointing to same memory address.
Therefore you get:
kid1.name == "C"
kid2.name == "C" 
 
    
    - 1,533
- 19
- 18
- 29
 
    
    - 1
- 2
There are 2 categories that are supported by Swift (Value Types, Reference Type). We have 3 different behaviours that we can have for those types - Copy by reference, Copy by value and Copy-on-Write. Classes as in your case are using copy by reference which means both instances point to same address - share a single copy of data. More details are described in my post about swift memory management and performance, I go as deep as binary values in the memory. I hope it helps: Swift Memory Management and Performance
 
    
    - 431
- 3
- 18
- 
                    2While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/24187835) – kenny_k Sep 30 '19 at 20:41
- 
                    @kenny_k I’ve added more context to my answer, I hope now it’s ok. – Michal Rogowski Oct 01 '19 at 06:33

 
     
    