Skip to content

Instantly share code, notes, and snippets.

@KyLeggiero
Created May 25, 2017 13:23
Show Gist options
  • Save KyLeggiero/d3d87fcfa0e3ac160df1ef6386ff676b to your computer and use it in GitHub Desktop.
Save KyLeggiero/d3d87fcfa0e3ac160df1ef6386ff676b to your computer and use it in GitHub Desktop.
/// Example type
public struct Foo {
// Fields...
}
/// Wraps `Foo` so it can be used for that static variable later
private struct FooWrapper {
static var shared = FooWrapper()
lazy var foo: Foo! = {
let foo = Foo()
// Setup...
return foo
}()
}
// MARK: - Usage
/// A global, "static", lazy-initialized, resettable varaible
public var foo: Foo! {
get {
return FooWrapper.shared.foo
}
set {
FooWrapper.shared.foo = newValue
}
}
// or:
/// A scoped, static, lazy-initialized, resettable varaible
struct Bar {
static var foo: Foo! {
get {
return FooWrapper.shared.foo
}
set {
FooWrapper.shared.foo = newValue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment