Discover how to ensure both functions are executed in Swift, even if the first returns `true`. Learn a simple workaround to achieve your desired results!
---
This video is based on the question https://stackoverflow.com/q/68758109/ asked by the user 'toto12344' ( https://stackoverflow.com/u/16093895/ ) and on the answer https://stackoverflow.com/a/68758232/ provided by the user 'Phil Dukhov' ( https://stackoverflow.com/u/3585796/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: swift - execute both func even the first return true in or statement
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Execute Both Functions in Swift, Even When the First Returns True
If you've been programming in Swift, you may have encountered a common scenario where you want to execute multiple functions, but the first function returns true, causing the subsequent functions to be skipped. This behavior is due to the way conditional statements evaluate in Swift, particularly with the logical OR (||) operator. Let's dive deeper into this issue and explore a straightforward solution.
The Problem
Consider the following example:
[[See Video to Reveal this Text or Code Snippet]]
When you run the run() function, you receive the following output:
[[See Video to Reveal this Text or Code Snippet]]
What’s Happening Here?
The first condition, test1(), returns true.
The logical OR statement (||) stops any further evaluation since it already knows the overall result will be true.
As a result, test2() is never called, and you only see "test1" printed, along with "something" from doSomething(), but you never see "test2".
Your Goal
You want to achieve the following output:
[[See Video to Reveal this Text or Code Snippet]]
The Solution
To ensure both functions are executed, regardless of the outcome of the first function, you can modify your if condition. Here’s how:
[[See Video to Reveal this Text or Code Snippet]]
Explanation of the Solution
Evaluation of Both Functions: By placing both function calls inside an array ([test1(), test2()]), Swift will evaluate each function independently.
Using contains(true): The contains(true) method checks the resulting array to see if at least one of the values is true. This ensures that even if the first function returns true, the second function will still run.
Benefits of This Approach
Code Clarity: Your intent is clear — both functions are executed and evaluated.
Flexibility: You can adapt this pattern to check more than two functions easily.
Maintainability: It keeps the code easy to read and understand.
Conclusion
In Swift, by default, functions are called lazily in conditional statements to optimize performance. However, when you need to execute all functions, regardless of their return value in a logical OR condition, you can utilize an array to enforce their evaluation. By using [test1(), test2()].contains(true), you ensure that both functions run, leading to your desired output.
Feel free to use this technique in your Swift projects whenever you need to perform similar checks!
Информация по комментариям в разработке