How to transfer the link to Boolean, but not to its value?

I want to use booleans as a state store.

To do this, I need to be able to change my state from the places of differences in the project.

To do this, I need to store them somewhere and a way to pass a link to them.

I tried to store them as static variables in the GameManager , but passing references to them only seems to pass true to false, not to the link.

How can I achieve this with a passive logical link . Can I change her condition from any part of the project?

UPDATE:

This is not the best way to do this, but it achieves the goal of having a bunch of logical states that I can use in the game world:

class GameManager { static let sharedInstance = GameManager() var previewAudioIsON: Bool = false var previewVisuaIsOn: Bool = false var timerDisplayIsOn: Bool = false var quickStartIsOn: Bool = false func touchedPreviewAudioButton() -> Bool { if previewAudioIsON { previewAudioIsON = false} else { previewAudioIsON = true } return previewAudioIsON } func touchedPreviewVisualButton() -> Bool { if previewVisuaIsOn { previewVisuaIsOn = false } else { previewVisuaIsOn = true } return previewVisuaIsOn } func touchedTimeDisplayButton() -> Bool { if timerDisplayIsOn { timerDisplayIsOn = false } else { timerDisplayIsOn = true } return timerDisplayIsOn } func touchedQuickStartButton() -> Bool { if quickStartIsOn { quickStartIsOn = false } else { quickStartIsOn = true } return quickStartIsOn } } 
+2
source share
2 answers

I gave you partially incorrect information the other day (I had a brain fart), and I need to apologize for that. I missed something in my testing ...

Here is what you need if you do not want to make RefBool instances, as I suggested (requires more work, not recommended):

 /// Mutates a boolean: func toggle(_ boolean: inout Bool) -> Bool { boolean ? (boolean = false) : (boolean = true) return boolean } /// Static state manager for Booleans struct IsOn { private static var _previewAudio = false, _previewVisual = false, _timerDisplal = false, _quickStart = false enum State { case toggle, get } static func previewAudio(_ toggleVal: State = .get) -> Bool { if toggleVal == .toggle { toggle(&_previewAudio) }; return _previewAudio } // ... others } 

Testing:

 let referenceToPA = IsOn.previewAudio print ( IsOn.previewAudio() ) // False (default pram works) print ( referenceToPA(.get) ) // False (can't use default pram) referenceToPA(.toggle) print ( IsOn.previewAudio() ) // True print ( referenceToPA(.get) ) // True IsOn.previewAudio(.toggle) print ( IsOn.previewAudio() ) // False print ( referenceToPA(.get) ) // False 



But to be honest, it would be easier just to make RefBool from my other answer, then you won't need an enumeration or functions:

 /// Holds a boolean in .val: final class RefBool { var val: Bool; init(_ boolean: Bool) { val = boolean } } /// Static state manager for Booleans struct IsOn { static var previewAudio = RefBool(false), previewVisual = RefBool(false), timerDisplal = RefBool(false), quickStart = RefBool(false) } 

Convenient functions (optional):

 /// Mutates a boolean: func toggle(_ boolean: inout Bool) -> Bool { boolean ? (boolean = false) : (boolean = true) return boolean } /// Mutates .val: func toggle(_ refBool: RefBool) -> Bool { refBool.val ? (refBool.val = false) : (refBool.val = true) return refBool.val } 

Testing2:

 let refToPA = IsOn.previewAudio refToPA.val = true print(refToPA.val) // true print(IsOn.previewAudio.val) // true toggle(&refToPA.val) print(refToPA.val) // false print(IsOn.previewAudio.val) // false toggle(refToPA) // Using our fancy second toggle print(refToPA.val) // true print(IsOn.previewAudio.val) // true 
+1
source

try something like this:

Full example

 import UIKit enum ButtonType { case PreviewAudio; case PreviewVisua; case TimerDisplay; case QuickStart; } class SwitchProperty { var type: ButtonType var value: Bool init (type: ButtonType) { self.type = type self.value = false } var description: String { var result = "type = \(type)\n" result += "value = \(value)" return result } func switchValue() { value.invertValue() } } class GameManager { static var previewAudioIsON = SwitchProperty(type: .PreviewAudio) static var previewVisuaIsOn = SwitchProperty(type: .PreviewVisua) static var timerDisplayIsOn = SwitchProperty(type: .TimerDisplay) static var quickStartIsOn = SwitchProperty(type: .QuickStart) } class Button: UIButton { var switchValue: SwitchProperty init (type: ButtonType, frame: CGRect) { switch type { case .PreviewAudio: switchValue = GameManager.previewAudioIsON case .PreviewVisua: switchValue = GameManager.previewVisuaIsOn case .TimerDisplay: switchValue = GameManager.timerDisplayIsOn case .QuickStart: switchValue = GameManager.quickStartIsOn } super.init(frame: frame) addTarget(self, action: #selector(Button.buttonTouched), for: .touchUpInside) } func buttonTouched() { switchValue.switchValue() print(switchValue.description) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } extension Bool { mutating func invertValue() { self = !self } } class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() addButton(type: .PreviewVisua, frame: CGRect(x: 40, y: 40, width: 200, height: 40)); addButton(type: .PreviewAudio, frame: CGRect(x: 40, y: 100, width: 200, height: 40)); } func addButton(type: ButtonType, frame: CGRect) { let button = Button(type: type, frame: frame); button.setTitleColor(UIColor.blue, for: .normal) button.setTitle("\(type)", for: .normal) view.addSubview(button) } } 

Accsess properties from another code

 // change value GameManager.previewAudioIsON.value = false // check type if (GameManager.previewAudioIsON.type == .PreviewAudio) { print("previewAudioIsON") } 

Result

enter image description here

0
source

Source: https://habr.com/ru/post/1260055/


All Articles