gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:09:08 30 4
gpt4 key购买 nike

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

我有带接口(interface)的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 只是每三秒更改参数 a 的值三次。当然,我创建了 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()
}
}

我预计 valueA 的 didSet block 会被调用三次,但在控制台中我只看到最后一次更改 valueA 的调用:“new valueA 100 on time 2018-03-26 11:50:18 +0000”。我可以做些什么来改变这种行为?

最佳答案

您必须在某处说“变量已更改”。例如,您可以使用闭包/ block /lambda 来完成

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

测试集set-Bridging-Header.h

#import "Wrapper.h"

关于c++ - Swift 并使用 &int 参数调用 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49491105/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com