【发布时间】:2020-11-22 12:44:29
【问题描述】:
请帮助我解决以下问题。我正在使用 swift 参数解析器库https://github.com/apple/swift-argument-parser
假设我在某个 swift 文件中有这个 swift 代码:
@objc public class Math: NSObject, ParsableCommand {
static var configuration = CommandConfiguration(
abstract: "A utility for performing maths.",
subcommands: [Add.self],
defaultSubcommand: Add.self)
}
extension Math {
struct Add: ParsableCommand {
static var configuration = CommandConfiguration(abstract: "Print the sum of the values.")
mutating func run() {
print("0")
}
}
}
` 我想调用一个名为“func”的objective-c ++函数,其参数类型为Math.Add,就像main.swift文件中的那样:
var status: Int32 = 0
do {
// try to parse application arguments
var args = try Math.parseAsRoot()
switch args {
case let args as Math.Add:
status = func(args)
default:
try args.run()
}
} catch {
//Some code...
}
函数的签名应该是什么以及桥接头文件应该是什么样子?我的签名是:
extern "C" int func(Math.Add *args);
而我的桥接头是这样的:
#ifndef __BRIDGING_HEADER_H__
#define __BRIDGING_HEADER_H__
@class Math;
#ifdef __cplusplus
extern "C" {
#endif
int func(Math.Add *args);
#ifdef __cplusplus
}
#endif
#endif /* __BRIDGING_HEADER_H__ */
但是它不起作用并且桥接头没有编译(Xcode 写错误:接口类型'Math' 不能按值传递;你忘记了'Math' 中的*?
【问题讨论】:
标签: swift objective-c++ bridging-header