如何正确使用__bridge的ARC

时间:2012-04-06 23:30:14

标签: iphone ios automatic-ref-counting

我将下面显示的代码更改为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的中断。 http://www.ecometrixem.com/cms-assets/documents/44729-919017.acapela-for-iphone.pdf

这是暗恋的截图。 enter image description here

解决 这段代码很有效,谢谢。

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];
    }
}

1 个答案:

答案 0 :(得分:6)

你需要这样的东西:

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