1

I'm trying to pass a UILabel with AudioServicesAddSystemSoundCompletion but i'm not able to manipulate the value within the completionCallback method. I'm using ARC and Xcode suggested adding (_bridge void*).

Any help would be much appreciated.

-(void) playWordSound:(UILabel *)label
{
    NSString *path;
    SystemSoundID soundId;
    switch (label.tag)
    {
        case 1:
            ..........
            break;
    }
    NSURL *url = [NSURL fileURLWithPath:path];
    AudioServicesCreateSystemSoundID( (CFURLRef)objc_unretainedPointer( url), &soundId);
    AudioServicesPlaySystemSound(soundId);
    AudioServicesAddSystemSoundCompletion (soundId, NULL, NULL, 
                                           completionCallback,
                                           (__bridge void*) label);
}


static void completionCallback (SystemSoundID  mySSID, void* data) {
    NSLog(@"completion Callback");
    AudioServicesRemoveSystemSoundCompletion (mySSID);
    //the below line is not working
    //label.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
}
garethdn
  • 12,022
  • 11
  • 49
  • 83

1 Answers1

3

In the completion handler the label is stored in data. You need to __bridge it back to use it.

static void completionCallback (SystemSoundID  mySSID, void* data) {
    NSLog(@"completion Callback");
    AudioServicesRemoveSystemSoundCompletion (mySSID);
    UILabel *label = (__bridge UILabel*)data;
    label.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
}
Joe
  • 56,979
  • 9
  • 128
  • 135
  • That's great thanks, it's working for me now. Could you briefly explain to a noob the idea behind bridging and what exactly it does? – garethdn May 16 '12 at 13:37
  • 1
    It was introduced in ARC to handle ref-counting when storing to a generic type like `void*`. See [ARC and bridged cast](http://stackoverflow.com/questions/7036350/arc-and-bridged-cast) – Joe May 16 '12 at 13:40
  • Joe, could you tell me how to call another method from within the `completionCallback` method. After i change the text color i've tried calling another method with `[self anotherMethod]`, but it's not working for me. – garethdn May 16 '12 at 15:00
  • Start up another question and just post a link here. Make sure you check to see if it has been answered already. – Joe May 16 '12 at 15:06