Skip to content

Instantly share code, notes, and snippets.

View dmytro-anokhin's full-sized avatar
🔨

Dmytro Anokhin dmytro-anokhin

🔨
  • London
View GitHub Profile
protocol Store {
func fetch(_ completion: @escaping (_ result: Model?) -> Void)
}
class LocalStore: Store {
func fetch(_ completion: @escaping (_ result: Model?) -> Void) {
// Perform database operation
}
class CompositeTableViewDataSource: UITableViewDataSource {
// ...
}
class CompositeCollectionViewDataSource: UICollectionViewDataSource {
// ...
}
class CompositeMapViewDelegate: MKMapViewDelegate {
// ...
// Initialization of lock, pthread_rwlock_t is a value type and must be declared as var in order to refer it later. Make sure not to copy it.
var lock = pthread_rwlock_t()
pthread_rwlock_init(&lock, nil)
// Protecting read section:
pthread_rwlock_rdlock(&lock)
// Read shared resource
pthread_rwlock_unlock(&lock)
// Protecting write section:
class ThreadSafeCollection<Element> {
// Concurrent synchronization queue
private let queue = DispatchQueue(label: "ThreadSafeCollection.queue", attributes: .concurrent)
private var _elements: [Element] = []
var elements: [Element] {
var result: [Element] = []
repeat {
let oldValue = // read value (1)
let newValue = // calculate new value
let success = compareAndSwap(old: oldValue,
new: newValue,
current: /* read value (2) */)
} while (!success)
protocol ThreadSafeContainer: AnyObject {
func read(_ closure: (_ object: Any?) -> Void)
func write(_ closure: (_ object: Any?) -> Any?)
}
class ShapeView: UIView {
var strokeColor: UIColor?
var fillColor: UIColor?
}
class RectangleView: ShapeView {
override func draw(_ rect: CGRect) {
// Drawing a rectangle
}
}
protocol ShapeViewDelegate {
func drawShapeView(_ shapeView: ShapeView)
}
class ShapeView: UIView {
var strokeColor: UIColor?
var fillColor: UIColor?
var delegate: ShapeViewDelegate? {
didSet {
@import Foundation;
NS_ASSUME_NONNULL_BEGIN
@class MyClass // Forward declaration required by the compiler
@protocol MyDelegate <NSObject>
- (BOOL)myClass:(MyClass *)myClass shouldProcessEvent:(id)event;
protocol MyDelegate : AnyObject {
func myClass(_ myClass: MyClass, shouldProcessObject object: Any) -> Bool
}
class MyClass : NSObject {
weak var delegate: MyDelegate?
}