I have a shared handler class in which I 'manage' objects.
In this shared class, there is a 'main object (mainObject)' and a 'single object (singleData)'.
If I now assign the singleData with the reference to mainObject.data[index] in viewA and then change mainObject.data[index] in viewB, then the singleData object also changes. How can I avoid this strong-reference here?
==> In short: I want to change the mainObject without changing the singleObject. <==
struct kOBJECT {
let name: String
let data: [Int]
}
class HandlerClass {
let shared = HandlerClass()
var mainObject = kOBJECT(name: "AnyName", data: [1,2,3,4,5])
var singleData: Int?
}
class viewA: UIViewController {
.....
func didSelectRow(at indexPath: IndexPath) {
HandlerClass.shared.singleData = HandlerClass.shared.mainObject.data[indexPath.row] // Create Reference
viewB.indexPath = indexPath
pushToViewController(viewB)
}
}
class viewB: UIViewController {
.....
public var indexPath: IndexPath!
func someFunction() {
HandlerClass.shared.mainObject.data[indexPath.row] = 10000 // <- Does this also change the `singleData` Reference? In my case it does......
}
}
I tried the following in 'didSelectRow'
let tempValue = HandlerClass.shared.mainObject.data[indexPath.row]
HandlerClass.shared.singleObject = tempValue

shared(singleton) object is to share data, if you don't want this then each view controller should have its own instance of HandlerClass