I have three classes A, B and C. A has a resource called rA. What I am trying to achieve is that all of those instances have a reference to the exact same resource.
So to concrete in Swift terms:
Class A has a property called foo:
private var foo : [Bar] = [Bar]() // shared resource
Class B has a property called foo which is passed into the initializer as an inout parameter:
private var foo : [Bar]!
init(inout foo:[Bar]){
self.foo = foo
}
Class C is analogous to Class B
How come, if I pass foo
from Class A to Class B (or C) the address changes ?
In A I would pass it to B (or C) like so:
let b = B(foo: &self.foo)
When I print the address after initialization of foo
in A it gives me a different address than after assignment in B.
class A{
private var foo = [Bar]()
func someFunc(){
NSLog("\n\n [A] \(unsafeAddressOf(self.foo))\n\n") // different from output in B
let b = B(foo: &self.foo)
}
}
class B{
private var foo: [Bar]!
init(inout foo: [Bar]){
self.foo = foo
NSLog("\n\n [B] \(unsafeAddressOf(self.foo))\n\n")
}
}
Any ideas why this is the case ?