0

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
2
  • The whole point of having a shared(singleton) object is to share data, if you don't want this then each view controller should have its own instance of HandlerClass Commented Feb 10, 2023 at 15:12
  • Your handler var is an instance var not a class var. Commented Feb 10, 2023 at 19:03

1 Answer 1

1

You misunderstand reference meaning. Reference works only of instances - that's a part of memory reserved for specific objects. Structs are also objects, but it works as Type (similar to Int, String, Double, Float, etc), so when you modify a struct as a result you will have a new object, but for class instance, you will still modify the same object as you copied only reference to that object.

Here example below:

class Apple {
    init(name: String) {
        self.name = name
    }
    var name: String
}

struct Pear {
    var name: String
}

var apple = Apple(name: "Apple 1")
var pear = Pear(name: "Pear 1")

var apple2 = apple // here we copy only reference
apple2.name = "Apple 2"
print(apple.name) // 1st object
print(apple2.name) // the same object

var pear2 = pear // here we create new object
pear2.name = "Pear 2"
print(pear.name) // 1st object
print(pear2.name) // 2nd object

Also a result (run on Playground)

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply, that opened my eyes. The struct was just an example here, in my app I actually use a class as an object. "var apple2 = apple" How can I prevent this from creating a reference?
If you need to create new object instead of copy reference, just initiate a new instance: var apple2 = Apple(name: apple.name).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.