I'm failing to pass an objective-c block to a swift function.

I copied files from Apple example code for Audio Unit V3 to a fresh project, and something broke.

There is an Objective-C function that passes an anonymous block to a Swift file/function -- the Swift func is no longer working because it's not receiving the block properly.

When I put a breakpoint inside the Swift function, the variable for the passed-in block shows as completionHandler (() -> ()) but with no address.

It should show completionHandler (()->()) 0xd8d87999 or whatever. (And it should pass this to another Swift function, which it is no longer able to call.)

The rest of the Swift code is working AFAIK.

Any ideas much appreciated.

The Objective-C function:
(trying to pass ^{[self connectParametersToControls];} to Swift object, "playEngine")

playEngine = [[SimplePlayEngine alloc] initWithComponentType: desc.componentType componentsFoundCallback: nil];
[playEngine selectAudioUnitWithComponentDescription_objc:desc
                                             [self connectParametersToControls];

The Swift function:

@objc public func selectAudioUnitWithComponentDescription_objc(_ componentDescription: AudioComponentDescription, completionHandler: @escaping (() -> Void)) {
      self.selectAudioUnitWithComponentDescription(componentDescription, completionHandler:completionHandler)

The relevant line in the Swift header:

- (void)selectAudioUnitWithComponentDescription_objc:(AudioComponentDescription)componentDescription completionHandler:(void (^ _Nonnull)(void))completionHandler;

1 Answers

alrightSpider On

It turns out although Obj-c optimization was off, the Swift optimization was on, and this was causing the problem. I don't suppose it should, but this seems to be the case.