Skip to content

Instantly share code, notes, and snippets.

@Styrmist
Created February 16, 2020 12:29
Show Gist options
  • Save Styrmist/cf8a95899bed866b12798d9fb667e54b to your computer and use it in GitHub Desktop.
Save Styrmist/cf8a95899bed866b12798d9fb667e54b to your computer and use it in GitHub Desktop.
run something only once
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