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
source share