Swift并使用& int参数调用C ++函数

时间:2018-03-26 12:07:57

标签: c++ objective-c swift

我研究如何在一个项目中使用C ++与Swift。

我有带接口

的C ++类
class SwypeDetect {
public:
  SwypeDetect();
  void asyncFunction(int &a);
};

和实施

SwypeDetect::SwypeDetect() {}

void SwypeDetect::asyncFunction(int &a) {
  a = 1;
  sleep(3);
  a = 10;
  sleep(3);
  a = 100;
}

asyncFunction只需每三秒更改一次参数值三次。当然我创建了Objective-C包装器

@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

然后在Swift类中使用这个包装器

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()
  }
}

我期望值​​A的didSet块会被调用三次,但是在控制台中我只看到最后一次更改值的调用A:“新值A 100准时2018-03-26 11:50:18 +0000”。我该怎么做才能改变这种行为?

1 个答案:

答案 0 :(得分:2)

你必须说'变量被改变'。你可以使用闭包/块/ lambdas来实现它,例如

<强> ViewController.swift

import UIKit

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) { value in
                        print("iteration value: \(value)")
                    }
                }
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        detector = Wrapper()
    } }

<强> Wrapper.h

#import <Foundation/Foundation.h>


@interface Wrapper : NSObject
- (instancetype) init;
- (void)asyncFunction:(int *)a progressHandler: (void(^)(int))progressHandler;
@end

<强> Wrapper.mm

#import "Wrapper.h"
#import "SwypeDetect.hpp"

@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 progressHandler: (void(^)(int))progressHandler {
    _detector.asyncFunction(*a , progressHandler);
}

@end

<强> SwipeDetect.cpp

#include "SwypeDetect.hpp"
#include <unistd.h>
#include <iostream>
SwypeDetect::SwypeDetect() {}

void SwypeDetect::asyncFunction(int &a, std::function<void(int)> f) {
    std::cout << "Come up and C++ me some time." << std::endl;
    a = 1;
    f(a);
    sleep(1);
    a = 10;
    f(a);
    sleep(1);
    a = 100;
    f(a);
}

<强> SwypeDetect.hpp

#include <stdio.h>
#include <functional>
class SwypeDetect {
public:
    SwypeDetect();
    void asyncFunction(int &a, std::function<void(int)> f);
};

<强> testsetsetset-桥接-Header.h

#import "Wrapper.h"