Swiftpack.co - dehlen/Atomic as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by dehlen.
dehlen/Atomic 1.0.0
Provides synchronized access to a variable
⭐️ 1
🕓 4 years ago
iOS macOS
.package(url: "https://github.com/dehlen/Atomic.git", from: "1.0.0")

Atomic

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.

Installation

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.

Documentation

The documentation is generated thanks to jazzy.

You can find the latest version here: Documentation.

Usage

/// 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)

License

The MIT License

Copyright (c) 2020 David Ehlen

See LICENSE file

GitHub

link
Stars: 1
Last commit: 4 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Release 1.0.0
4 years ago
  • fix config (f76b2be)
  • add release it (a60bbd2)
  • rewrite documentation (bc0f9db)
  • add documentation folder (760bf65)
  • add docs - remove workflow (7072dae)
  • fix workflow (3cf8419)
  • fix workflow (9568987)
  • fix workflow! (ed9486d)
  • add documentation section to readme (b428d57)
  • add documentation github action (6f9d3a1)
  • Add LICENSE file (47dafee)
  • initial (2b26e79)
  • Initial Commit (2e78196)

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics