【发布时间】:2020-05-31 20:08:18
【问题描述】:
如何在 macOS Bundle 之间传递数据?
例如:MyApp.app 将数据发送到 MyBundle.bundle 中的函数或类初始化程序,后者对其执行自己的逻辑。它甚至可以返回一个值,然后 MyApp.app 可以进一步处理。
例如,MyBundle 中的一个函数(不嵌套在任何其他声明中):
void initialise(unsigned int frameCount) {…} // not a class initialiser btw
我试过了:
-
在 MyBundle 中声明一个类,然后使用
Foundation.Bundle在 MyApp 中加载它:let class = (Bundle(url: url)!.classNamed("MyClass")! as! NSObject.Type).init() // class is an instance of NSObject, but I can override the `init()` method // (in MyBundle) to do whatever. I have not found a way to implement an // initialiser with parameters, and it still be recognised here. -
在 MyBundle 中声明一个函数并使用
CFBundleGetFunctionPointerForName来检索它。var bundles = CFBundleCreateBundlesFromDirectory(kCFAllocatorDefault, bundleDirectory, bundleExtension)! as Array var bundle1 = bundles.first! as! CFBundle var untypedFunctionPointer = CFBundleGetFunctionPointerForName(bundle, "initialise" as CFString)! let imp = unsafeBitCast(untypedFunctionPointer, to: IMP.self) let function = unsafeBitCast(imp, to: (@convention(c)(CUnsignedInt) -> Void).self) // If I specify parameters here, pass them in below, then try to use them in my objective c function, I get exc_bad_access. function(CUnsignedInt(1.0)) // Will work only when not trying to access parameters in the original objective c function. // Whatever @convention(c) function signature I use, the parameters are unusable in Objective C.
两者的关键问题是我无法将数据作为参数传递。所以打印语句可以工作,但不能使用参数。
编辑:接受的答案显示调用函数指针的正确方法。我谈到的崩溃是由于使用了没有正确桥接到 C 语言家族的类型的结果,所以如果有疑问,请坚持使用 Foundation 库中的 NSNumber、NSString 等。
【问题讨论】:
标签: swift bundle foundation