In Objective-C you have a distinction between atomic and nonatomic properties:
@property (nonatomic, strong) NSObject *nonatomicObject;
@property (atomic, st
From Swift 5.1 you can use property wrappers to make specific logic for your properties. This is atomic wrapper implementation:
@propertyWrapper
struct atomic {
private var value: T
private let lock = NSLock()
init(wrappedValue value: T) {
self.value = value
}
var wrappedValue: T {
get { getValue() }
set { setValue(newValue: newValue) }
}
func getValue() -> T {
lock.lock()
defer { lock.unlock() }
return value
}
mutating func setValue(newValue: T) {
lock.lock()
defer { lock.unlock() }
value = newValue
}
}
How to use:
class Shared {
@atomic var value: Int
...
}