首页 > 解决方案 > 为什么我的 Objective-C 块在传递给 Swift 函数时没有显示?

问题描述


我未能将objective-c 块传递给swift 函数。


我将音频单元 V3 的 Apple 示例代码中的文件复制到一个新项目中,结果出现问题。

有一个 Objective-C 函数将匿名块传递给 Swift 文件/函数——Swift func 不再工作,因为它没有正确接收块。

当我在 Swift 函数中放置断点时,传入块的变量显示为completionHandler (() -> ())但没有地址。

它应该显示completionHandler (()->()) 0xd8d87999或其他。(它应该将它传递给另一个 Swift 函数,它不再能够调用它。)

Swift 代码的其余部分正在运行 AFAIK。

任何想法都非常感谢。


Objective-C 函数:(
试图传递^{[self connectParametersToControls];}给 Swift 对象,“playEngine”)

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


斯威夫特函数:

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


Swift 标头中的相关行:

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

标签: swiftmacosobjective-c-blockscore-audioaudiounit

解决方案


事实证明,虽然 Obj-c 优化已关闭,但 Swift 优化已打开,这导致了问题。我不认为它应该,但似乎是这样。


推荐阅读