Mike Nathas
Mike Nathas

Reputation: 1393

Use a Pointer to an object in Swift class

I've created a class which should have a variable containing a pointer to a Swift object.

class dataCollector: NSObject {
  var dataDict : [String:Int] = ["value1":10]
  ...(class will update values inside dataDict regularly)...
}

class dataDisplay: NSObject {
  var name : String = "Name"
  var object : dataCollector? = nil
}

I'm using this to instantiate the dataDisplay object

var collector1 = dataCollector()
...
var display1 = dataDisplay()
...
display1.object = collector1

I'm wondering of object in display1 contains a copy of collector1 or a pointer to collector1.

I set a binding to display1.object.value1 which isn't updated in the UI although it is updated in the dataCollector object (I've checked this via some print() output)

If object is indeed no pointer how can I add a pointer variable to the class?

Thanks!

Upvotes: 1

Views: 1327

Answers (1)

Alexander
Alexander

Reputation: 63227

collector1 and display1.object are both referencess to the same dataCollector object. This is the case for all reference (class) types. Value types on the other hand (enums, tuples, structs) are all copied on assignment.

Upvotes: 1

Related Questions