This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol Store { | |
| func fetch(_ completion: @escaping (_ result: Model?) -> Void) | |
| } | |
| class LocalStore: Store { | |
| func fetch(_ completion: @escaping (_ result: Model?) -> Void) { | |
| // Perform database operation | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CompositeTableViewDataSource: UITableViewDataSource { | |
| // ... | |
| } | |
| class CompositeCollectionViewDataSource: UICollectionViewDataSource { | |
| // ... | |
| } | |
| class CompositeMapViewDelegate: MKMapViewDelegate { | |
| // ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] = [] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| repeat { | |
| let oldValue = // read value (1) | |
| let newValue = // calculate new value | |
| let success = compareAndSwap(old: oldValue, | |
| new: newValue, | |
| current: /* read value (2) */) | |
| } while (!success) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol ThreadSafeContainer: AnyObject { | |
| func read(_ closure: (_ object: Any?) -> Void) | |
| func write(_ closure: (_ object: Any?) -> Any?) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ShapeView: UIView { | |
| var strokeColor: UIColor? | |
| var fillColor: UIColor? | |
| } | |
| class RectangleView: ShapeView { | |
| override func draw(_ rect: CGRect) { | |
| // Drawing a rectangle | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol ShapeViewDelegate { | |
| func drawShapeView(_ shapeView: ShapeView) | |
| } | |
| class ShapeView: UIView { | |
| var strokeColor: UIColor? | |
| var fillColor: UIColor? | |
| var delegate: ShapeViewDelegate? { | |
| didSet { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @import Foundation; | |
| NS_ASSUME_NONNULL_BEGIN | |
| @class MyClass // Forward declaration required by the compiler | |
| @protocol MyDelegate <NSObject> | |
| - (BOOL)myClass:(MyClass *)myClass shouldProcessEvent:(id)event; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol MyDelegate : AnyObject { | |
| func myClass(_ myClass: MyClass, shouldProcessObject object: Any) -> Bool | |
| } | |
| class MyClass : NSObject { | |
| weak var delegate: MyDelegate? | |
| } |