Created
May 25, 2017 13:23
-
-
Save KyNorthstar/d3d87fcfa0e3ac160df1ef6386ff676b to your computer and use it in GitHub Desktop.
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
/// 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