AtomicValue
public final class AtomicValue<Value>
A class that wraps access to its underlying value with an NSLocking instance.
-
Declaration
Swift
public init(initialValue: Value)
-
Declaration
Swift
public func get() -> Value
-
Declaration
Swift
public func set(_ newValue: Value)
-
Sets AtomicValue to
newValue
and returns the old valueDeclaration
Swift
public func getAndSet(_ newValue: Value) -> Value
-
Performs
block
with the current value, preventing other access until the block exits.Declaration
Swift
public func atomicallyPerform(block: (Value) -> Void)
-
Performs
block
with aninout
value, preventing other access until the block exits, and enabling the block to mutate the valueWarning
The AtomicValue lock is not reentrant. Specifically, it is not possible to call outside the block toget
an AtomicValue (e.g., via a convenience property) while inside thewith
block. Attempting to do so will cause a deadlock.Declaration
Swift
public func with(block: (inout Value) -> Void)
-
Toggles the boolean’s value, and returns the old value.
Example:
let atomicBool = AtomicValue(initialValue: true) print(atomicBool.getAndToggle()) // prints "true" print(atomicBool.get()) // prints "false"
Declaration
Swift
public func getAndToggle() -> Value
-
Increments the current value by
amount
and returns the incremented valueDeclaration
Swift
public func increment(by amount: Value = 1) -> Value
-
Decrements the current value by
amount
and returns the decremented valueDeclaration
Swift
public func decrement(by amount: Value = 1) -> Value
-
Declaration
Swift
public func append(_ newElement: Value.Element)
-
Declaration
Swift
public func append<S>(contentsOf sequence: S) where S : Sequence, Value.Element == S.Element
-
Declaration
Swift
public func removeFirst() -> Value.Element
-
Declaration
Swift
public subscript(key: Value.Index) -> Value.Element { get }