Toll-free bridge question


#1

I’ve been trying to understand the __bridge cast modifier. Am I right in my assumption that the lines:

    CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
    CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
    NSString *key = (__bridge NSString *)newUniqueIDString;
    [item setImageKey:key];
    [[BNRImageStore sharedStore] setImage:image forKey:[item imageKey]];
    CFRelease(newUniqueIDString);

Are equivalent to this:

    CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
    CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
    NSString *key = (__bridge_transfer NSString *)newUniqueIDString;
    [item setImageKey:key];
    [[BNRImageStore sharedStore] setImage:image forKey:[item imageKey]];

Where the [color=#0000FF]__bridge[/color] was change to [color=#0000FF]__bridge_transfer[/color] and [color=#0000FF]CFRelease(newUniqueIDString)[/color] was omitted?

I’m trying to understand why in the book on page 254 [color=#0000FF]__bridge[/color] was chosen and not [color=#0000FF]__bridge_transfer[/color]. I understand that we still need to release the other CF object with CFRelease(newUniqueID);

Also is there a specific reason why in:[[BNRImageStore sharedStore] setImage:image forKey:[item imageKey]];
[color=#0000FF][item imageKey][/color] was chosen over just using [color=#0000FF]key[/color]?


#2
  1. I wanted to show the actual release statements that would have to be omitted if we bridged in another way.

  2. [item imageKey] is more correct because it is possible that -[BNRItem setImageKey:] could modify the imageKey. It doesn’t. But it is possible.


#3

I agree it is possible; however, it’s an extra method call vs. in-memory variable.

Is it negligible? What if I wanted to store multiple images at once. x method key calls for x images.

Just curious or is this more a philosophical discussion?


#4

The difference is negligible. Less than nanoseconds. You’d have to do it a few million times for a profiling tool to even pick up on it, much less a human.


#5

Good to know. Thanks for the clarification.