【问题标题】:How to use __bridge for ARC correctly如何正确使用 __bridge 进行 ARC
【发布时间】:2012-04-06 23:30:14
【问题描述】:

我将下面显示的代码更改为与 ARC 兼容。

我只是按照 Xcode 的建议对其进行了更改,并且在 Xcode 上没有显示错误。但是一旦事件发生,代码就会崩溃。有没有人想办法解决这个问题?

我不确定这种迷恋是否是因为 acapela SDK 而发生的。

这是非 ARC 代码,可以正常工作。

void MyInterruptionListener(void *inClientData, UInt32 inInterruptionState) {

    AcapelaSpeech* anAcapelaSpeech = *(AcapelaSpeech**)inClientData;

    if (inInterruptionState == kAudioSessionBeginInterruption) {

        [anAcapelaSpeech setActive:NO];
        status = AudioSessionSetActive(NO);
    }
    if (inInterruptionState == kAudioSessionEndInterruption) {

        status = AudioSessionSetActive(YES);
        [anAcapelaSpeech setActive:YES];
    }
}

这是与 ARC 兼容的,但它对 [anAcapelaSpeech setActive:NO]; 很不利。

void MyInterruptionListener(void *inClientData, UInt32 inInterruptionState) {

    AcapelaSpeech* anAcapelaSpeech = (__bridge_transfer AcapelaSpeech*)inClientData;

    if (inInterruptionState == kAudioSessionBeginInterruption) {

        [anAcapelaSpeech setActive:NO];
        AudioSessionSetActive(NO);
    }
    if (inInterruptionState == kAudioSessionEndInterruption) {

        AudioSessionSetActive(YES);
        [anAcapelaSpeech setActive:YES];
    }
}

其他信息。 我正在使用 Acapela 音频 SDK,音频中断代码显示在此 PDF 的 9.Interruptions 中。 http://www.ecometrixem.com/cms-assets/documents/44729-919017.acapela-for-iphone.pdf

这是暗恋的截图。

已解决 此代码有效,谢谢。

void MyInterruptionListener(void *inClientData, UInt32 inInterruptionState) {

    AcapelaSpeech *anAcapelaSpeech = (__bridge id) (*(void **) inClientData);

    if (inInterruptionState == kAudioSessionBeginInterruption) {

        [anAcapelaSpeech setActive:NO];
        AudioSessionSetActive(NO);
    }
    if (inInterruptionState == kAudioSessionEndInterruption) {

        AudioSessionSetActive(YES);
        [anAcapelaSpeech setActive:YES];
    }
}

【问题讨论】:

  • __bridge_retained 用于 CF 对象和需要保留对它们的引用的对象。 __bridge 用于不需要保留的 NSObject。
  • 感谢您的解释。 :)

标签: iphone ios automatic-ref-counting


【解决方案1】:

你需要这样的东西:

id asObject = (__bridge id) (*(void **) ptr);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多