NSLock-Synchronized: Do you miss @synchronized in Swift?

Do you miss @synchronized in Swift?

文件列表

NSLock+Synchronized
Sources
.gitignore
.swift-version
CHANGELOG.md
LICENSE.txt
NSLock+Synchronized.podspec
Package.swift
README.md

NSLock+Synchronized

Do you miss @synchronized in Swift? NSLock+Synchronized gives you back @synchronized in Swift via NSLock class and instance methods, conveniently usable via Cocoapod and Carthage Framework.

Installation

NSLock+Synchronized is available via Cocoapods (‘pod NSLock+Synchronized’) and can be built with Carthage. Once installed, you can import the module ‘NSLock_Synchronized’ in any class you need the synchronized methods to be available.

Use

Via NSLock Class (aka “Classic Mode”)

For the closest experience to @synchronized you can get in Swift. Like @synchronized in Objective-C, locking is done based on a hash table matching lock tokens to lock instances.

NSLock.synchronized(token) {
    self.synchronizedCounter += 1
}

Via NSLock Instance (aka “Ruby Mode”)

let lock = NSLock()
lock.synchronized {
    self.synchronizedCounter += 1
}

License

NSLock+Synchronized is available under the MIT license. See LICENSE.txt for details.

下载地址


Download ZIP