This package implements an Atomic class that uses os_unfair_lock (iOS) or pthread_mutex_t (macOS) to provide synchronized access to a variable.
The implementation was extracted from ReactiveSwift.
Currently only Swift Package Manager is supported. Swift Package Manager is a dependency manager built into Xcode.
If you are using Xcode 11 or higher, go to File / Swift Packages / Add Package Dependency... and enter package repository URL https://github.com/dehlen/Atomic.git, then follow the instructions.
To remove the dependency, select the project and open Swift Packages (which is next to Build Settings). You can add and remove packages from this tab.
The documentation is generated thanks to jazzy.
You can find the latest version here: Documentation.
/// Initialize the variable with the given initial value.
private var myVariable: Atomic<Bool> = Atomic(false)
/// Atomically get, set or modify the value of the variable.
print(myVariable.value)
myVariable.value = true
/// Atomically modifies the variable.
///
/// - parameters:
/// - action: A closure that takes the current value.
///
/// - returns: The result of the action.
myVariable.modify { oldValue in
if oldValue {
return false
} else {
return true
}
}
/// Atomically perform an arbitrary action using the current value of the
/// variable.
///
/// - parameters:
/// - action: A closure that takes the current value.
///
/// - returns: The result of the action.
myVariable.withValue { currentValue in
return false
}
/// Atomically replace the contents of the variable.
///
/// - parameters:
/// - newValue: A new value for the variable.
///
/// - returns: The old value.
let oldValue = myVariable.swap(true)
The MIT License
Copyright (c) 2020 David Ehlen
See LICENSE file
link |
Stars: 1 |
Last commit: 4 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics