How can I pass data between macOS Bundles?
eg: MyApp.app sends data to a function or class initialiser in MyBundle.bundle, which performs its own logic on it. It could even return a value, which MyApp.app can then process further.
Fo example, a function in MyBundle (not nested in any other declaration):
void initialise(unsigned int frameCount) {…} // not a class initialiser btw
I have tried:
Declaring a class in MyBundle and then loading it in MyApp using
Foundation.Bundle
: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.
Declaring a function in MyBundle and using
CFBundleGetFunctionPointerForName
to retrieve it.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.
The critical issue with both is that I am unable to pass data in as parameters. So a print statement will work but no ability to use arguments.
Edit: Accepted answer shows the correct method for calling function pointers. The crashes I talked about are a result of using types which are not being bridged to the C family of languages properly, so stick to NSNumber, NSString etc. from the Foundation library when in doubt.