I study how to use C++ with Swift in one project.
I have C++ class with interface
class SwypeDetect { public: SwypeDetect(); void asyncFunction(int &a); }; and implementation
SwypeDetect::SwypeDetect() {} void SwypeDetect::asyncFunction(int &a) { a = 1; sleep(3); a = 10; sleep(3); a = 100; } asyncFunction just change value of argument a three time each three seconds. Of course I create Objective-C wrapper
@interface Wrapper() @property (nonatomic, assign) SwypeDetect detector; @end @implementation Wrapper - (instancetype) init { self = [super init]; if (self) { _detector = SwypeDetect(); } return self; } - (void)asyncFunction:(int *)a { _detector.asyncFunction(*a); } @end And then use this wrapper in Swift class
class ViewController: UIViewController { let queue = DispatchQueue(label: "wrapper queue", attributes:.concurrent) var valueA: Int32 = 0 { didSet { print("new valueA \(valueA) on time \(Date())") } } var detector: Wrapper? { didSet { if let detector = detector { queue.async { detector.asyncFunction(&self.valueA) } } } } override func viewDidLoad() { super.viewDidLoad() detector = Wrapper() } } I expect that didSet block of valueA would be call three times, but in console I see only call with last change of valueA: "new valueA 100 on time 2018-03-26 11:50:18 +0000". What could I do to change this behaviour?
detector.asyncFunction(&self.valueA).