Skip to content

Instantly share code, notes, and snippets.

View tom-biel's full-sized avatar
🏠
Working from home

Tom Biel tom-biel

🏠
Working from home
View GitHub Profile
@tom-biel
tom-biel / NSGestureRecognizer_CallbackExtension.txt
Last active October 19, 2019 22:49
Add ability for create NSGestureRecognizer with closure
import Cocoa
extension NSGestureRecognizer {
private class CallbackWrapper {
let callback: NSGestureRecognizer.Callback
weak var gestureRecognizer: NSGestureRecognizer?
init(callback:@escaping NSGestureRecognizer.Callback) {
@tom-biel
tom-biel / synchronized_swift
Last active October 19, 2019 23:00
Objectice-C @synchronized replacement for Swift
@inline(__always)
func synchronized<T>(_ lock: AnyObject, _ body: () throws -> T) rethrows -> T {
objc_sync_enter(lock)
defer { objc_sync_exit(lock) }
return try body()
}
// example of usage
synchronized (self) {[weak self] in
@tom-biel
tom-biel / Atomic-propertyWrapper
Created October 26, 2019 03:33
Atomic property wrapper in Swift
@propertyWrapper
struct Atomic<Value> {
var lock = os_unfair_lock_s()
var _value: Value
init(wrappedValue value: Value) {
_value = value
}