【问题标题】:Why is my Objective-C block not showing up when passed to Swift function?为什么我的 Objective-C 块在传递给 Swift 函数时没有显示?
【发布时间】:2019-04-20 22:35:09
【问题描述】:

我未能将 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];
                                          }];


Swift 函数:

@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;

【问题讨论】:

    标签: swift macos objective-c-blocks core-audio audiounit


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多