vbezhenar vbezhenar - 4 months ago 32
Swift Question

How to pass structure by reference?

If I have some existing struct, but I want to use "Reference" behavior, how do I achieve that?

I can write some simple class-holder like

class Box<T> {
var value: T
init(_ value: T) {
self.value = value
}
}


I guess there must be ready class in the standard library, but I didn't find it.

I want to store that reference in my class, so inout parameter isn't what I need.

Answer

For me the best variant was using class-holder:

class Ref<T> {
  var value: T

  init(_ value: T) {
    self.value = value
  }
}