I have two objects (Child1, Child2) that are supposed to write values (UInt8) into the same Array<UInt8>. For this, there is one object (Parent) that owns both these children. This parent creates an Array<UInt8> and sets this array as a property on Child1 and Child2, like so:
class Child
{
var values: [UInt8] = []
func changeAValue()
{
values[2] = 42
}
}
class Parent
{
var sharedValues: [UInt8] = []
let child1 = Child()
let child2 = Child()
init()
{
sharedValues = [1,2,3,4,5]
child1.values = sharedValues
child2.values = sharedValues
child1.changeAValue()
print("Child1:\t\(child1.values)")
print("Child2:\t\(child2.values)")
print("Parent:\t\(self.sharedValues)")
}
}The output here is:
Child1: [1, 2, 42, 4, 5]
Child2: [1, 2, 3, 4, 5]
Parent: [1, 2, 3, 4, 5]I would like that these values are shard between these objects, so that the output would be ` [1, 2, 42, 4, 5]` for all three print statements. Of course I could use an NSMutableArray, but I am interested to know what would be a nice (and performant!) way to do this in pure Swift.
My (maybe naive?) idea is to just write a wrapper class for an Array, but this seems like a problem that smarter minds then me have already thought about and figured out the best solution for .
What is the best (most performant and generic) way to do this?