Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ide/synchronized
Exposes Objective-C's @synchronized directive to Swift
https://github.com/ide/synchronized
Last synced: 7 days ago
JSON representation
Exposes Objective-C's @synchronized directive to Swift
- Host: GitHub
- URL: https://github.com/ide/synchronized
- Owner: ide
- License: mit
- Created: 2014-09-16T20:16:34.000Z (about 10 years ago)
- Default Branch: main
- Last Pushed: 2021-03-18T19:32:12.000Z (over 3 years ago)
- Last Synced: 2024-09-17T08:14:54.251Z (about 2 months ago)
- Language: Swift
- Homepage:
- Size: 85 KB
- Stars: 51
- Watchers: 7
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Synchronized ![Tests](https://github.com/ide/synchronized/actions/workflows/tests.yml/badge.svg)
============[![Version](https://img.shields.io/cocoapods/v/Synchronized.svg?style=flat)](http://cocoadocs.org/docsets/Synchronized)
[![License](https://img.shields.io/cocoapods/l/Synchronized.svg?style=flat)](http://cocoadocs.org/docsets/Synchronized)
[![Platform](https://img.shields.io/cocoapods/p/Synchronized.svg?style=flat)](http://cocoadocs.org/docsets/Synchronized)Exposes Objective-C's @synchronized directive to Swift. Like the Objective-C directive, Synchronized acquires a mutex lock, runs some code, and releases the lock when the code completes or throws an exception.
Linking the Framework
---Synchronized is available through [CocoaPods](http://cocoapods.org). To install it, simply add the following line to your Podfile:
pod "Synchronized", "~> 4.0"
You can also use [Carthage](https://github.com/Carthage/Carthage) to fetch it from GitHub:
github "ide/Synchronized" ~> 4.0
Once the framework is linked this Swift code should compile:
```swift
import Synchronizedlet x = synchronized(NSObject()) { 0 }
```Public API
---```swift
public func synchronized(object: AnyObject, closure: () -> Void)
```**Usage:**
```swift
synchronized(mutexObject) {
// Code to run in your critical section
}
```---
```swift
public func synchronized(object: AnyObject, closure: () -> T) -> T
```**Usage:**
```swift
let value = synchronized(threadUnsafeDictionary) {
threadUnsafeDictionary[key]
}
```Differences from `@synchronized`
---Objective-C's `@synchronized` is a language-level directive and does not introduce a new function scope. This means that `return` statements cause the program to return from the surrounding function that contains the `@synchronized` directive.
```objc
- (void)returnDifferenceExample
{
@synchronized {
return;
}
NSLog(@"This line of code does not run.");
}
```In contrast, Synchronized uses closures which do introduce a function scope. Returning from a closure passed to `synchronized` exits only the closure, not the surrounding function.
```swift
func returnDifferenceExample() {
synchronized {
return
}
println("This line of code does run.")
}
```Synchronized's closures are annotated with the `@noclosure` attribute, which removes the need to access instance variables with `self.`, so it is similar to Objective-C's `@synchronized` directive in this regard.