-4

I’m wondering if there is a way to put a one shot in swift programming? If your familiar with PLC ladder logic you will know what I mean by a one shot.

Thank you in advance!

  • 5
    Provide an example, what do you mean by one shot, otherwise most swift programmer will be unable to answer this question. – Ratul Sharker May 10 '21 at 18:27
  • 2
    You've had two out of three questions closed, please read the [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) article before asking another, and I strongly suggest that you put some effort into finding an answer before asking here. For example, you could have tried to find what the equivalent of a "one shot" is in Swift, and that probably would have led you to an answer. – EmilioPelaez May 10 '21 at 18:52

1 Answers1

0

I am not familiar with a specific function within Xcode that will achieve this however you can perform a sequential logic that depends on a variables state. Where bool1 is responsible for starting the sequence, bool2 is the latch/reset and var1 is the variable you wish to modify (or represents a section of code you only want performed once.)


var bool1 = false
var bool2 = false
var var1 = ""

if (bool1 == true && bool2 == true)
{
    var1 = "run code"
    bool2 = false
}

hope this helps?

  • Yes!!! This definitely provides a means for what I’m trying to achieve. – Code-bear456 May 10 '21 at 18:34
  • 2
    I think you meant `(bool1 == true && bool2 == true)` (double equal sign to check for equality) – aheze May 10 '21 at 18:40
  • 2
    I don’t believe this would be reliable or thread safe. It sounds to me like the OP is looking for `dispatch_once`. – matt May 10 '21 at 19:01
  • 1
    hey Matt. I think this sounds like a viable solution but it sounds like this is something that can only be run once and never again? https://stackoverflow.com/questions/37801407/whither-dispatch-once-in-swift-3 is that correct? I don't know if you are aware of what a one shot is but basically its a reusable "pulse" that activates and runs, then shuts itself off immediately after. – Michael Szabo May 10 '21 at 19:04