如何传递对布尔值而不是其值的引用? [英] How to pass a reference to a Boolean rather than its value?

查看:224
本文介绍了如何传递对布尔值而不是其值的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用布尔值作为状态存储.

I want to use booleans as state storage.

为此,我需要能够从项目中不同位置更改它们的状态.

In order to do that, I need to be able to change their state from difference places in the project.

为此,我需要在某个地方存储它们,并提供一种将引用传递给它们的方法.

To do that, I need somewhere to store them, and a way to pass a reference to them.

我尝试将它们存储为静态变量GameManager ,但是传递对它们的引用似乎只能传递true或false的值,而不是引用.

I've tried storing them as static variables in a GameManager, but passing references to these only seems to pass the value of true of false, not a reference.

如何实现拥有可传递的布尔引用的目标,我可以从项目的任何部分更改其状态?

How do I achieve this goal of having a passable boolean reference I can change the state of it from any part of the project?

这不是最好的方法,但这可以达到在游戏世界中使用一堆状态布尔值的目的:

This can't be the best way to do this, but this achieves the goal of having a bunch of state booleans that I can use around 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
    }
}

推荐答案

前几天,我向您提供了部分错误的信息(我有脑部放屁),因此需要为此道歉.我在测试中忽略了某些东西...

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

如果您不想按照我的建议制作RefBool实例,这是您需要的(需要更多的支腿工作,不建议这样做):

Here is what you need if you don't want to make the RefBool instances as I suggested (requires more legwork, 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
}


测试:

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




但是说实话,仅从我的其他答案中执行RefBool会更容易,那么您就不需要枚举或函数了:




But honestly, it would be easier to just do the RefBool from my other answer, then you wouldn't need the enum or the 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)
}

便捷功能(不必要):

/// 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
}

测试2:

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

这篇关于如何传递对布尔值而不是其值的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆