Created
February 16, 2020 12:29
-
-
Save Styrmist/cf8a95899bed866b12798d9fb667e54b to your computer and use it in GitHub Desktop.
run something only once
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| public class Once { | |
| var already: Bool = false | |
| public init() { } | |
| public func run(_ block: () -> Void) { | |
| guard !already else { | |
| return | |
| } | |
| block() | |
| already = true | |
| } | |
| } | |
| ////// | |
| let once = Once() | |
| once.run { | |
| // do something | |
| } | |
| once.run { | |
| // no effect | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment