Is it possible to point class instance variables to `inout` parameters in Swift?

muZero

Is it possible for multiple class methods to access and modify a single inout parameter that is set in the class constructor? For example something like this:

class X {
    var mySwitch: Bool
    
    init(mySwitch: inout Bool) {
        self.mySwitch = mySwitch
    }
    
    func updateSwitch() {
        self.mySwitch.toggle() // this should toggle the external Boolean value that was originally passed into the init
  }
}

// usage
var myBool: Bool = false
let x = X(mySwitch: &myBool)
x.updateSwitch()
print(myBool) // this should read 'true'
Jake

Short Answer

No.

Long Answer

There are other approaches that can satisfy this.

Binding Variables

In SwiftUI we use Binding Variables to do stuff like this. When the Binding variable updates, it also updates the bound variable. I'm not sure if it will work in Sprite Kit.

class X {
    var mySwitch: Binding<Bool>

    init(_ someSwitch: Binding<Bool>) {
        self.mySwitch = someSwitch 
    }

   func toggle() { mySwitch.wrappedValue.toggle() }
}

struct Y {
    @State var mySwitch: Bool = false
    lazy var switchHandler = X($mySwitch)
} 

Callbacks

We can add a callback to X and call it on didSet of the boolean.

class X {
    var mySwitch: Bool {
        didSet { self.callback(mySwitch) } // hands the new value back to the call site in Y
    }

    let callback: (Bool) -> Void

    init(_ someSwitch: Bool, _ callback: @escaping (Bool) -> Void) {
        self.mySwitch = someSwitch
        self.callback = callback
    }

    func toggle() { mySwitch = !mySwitch } // explicitly set to trigger didSet
}

class Y {
    var mySwitch: Bool = false
    lazy var switchHandler = X(mySwitch) {
        self.mySwitch = $0 // this is where we update the local value
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is it possible to create an array of functions that take "inout" parameters in Swift?

Swift optional inout parameters and nil

Swift closure capture and inout variables

Swift 2.0 'inout' function parameters and computed properties

Variable capture by closures in Swift and inout parameters

Swift memory address of outside instance of inout parameter same as copied instance

Swift/iOS: How to use inout parameters in functions with AnyObject/Any or Pointers

Is there any difference between "mutating" function and "inout" parameters in Swift?

Assigning value to inout parameters within closure in Swift 3

Is it possible to point to a class?

Swift override instance variables

Class instance of two variables

Hiding instance variables of a class

Singleton class and instance variables

python: class variables and instance variables

Class variables, instance variables and inheritance

Is it possible to modify variables in different class from within App() without instantiating new instance of class

When to use inout parameters?

IN, OUT, INOUT Parameters

Accessing Objective-c base class's instance variables from a Swift class

Is it possible to point to anonymous class by .THIS keyword?

Does this point to the class or a class instance? (6)

Is it possible to make a instance of a class callable

Is it possible to patch a class instance with an existing Singleton instance?

Class Level Instance Variables in Ruby

Reopening class and adding instance variables

Mixing class and instance variables together

Python: understanding class and instance variables

Ruby class instance variables in subclasses