-2

I am following a tutorial in Core Graphics and I came across the code __bridge. I looked into it and saw that it had to do with ARC but I am confused on what it does. Could someone explain what it does in this context?

    void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = { 0.0, 1.0 };

    NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];

    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user1588928
  • 11
  • 1
  • 2
  • 2
    It transfers ownership. And I hope there's more to that method or you have at least two leaks. – Kevin Dec 31 '13 at 19:44
  • 2
    I suggest to read "Managing Toll-Free Bridging" in the [Transitioning to ARC Release Notes](https://developer.apple.com/library/mac/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html) ! – Martin R Dec 31 '13 at 19:47
  • 3
    @Kevin Not to split hairs, but `__bridge` explicitly does not transfer "ownership". As [Transitioning to ARC Release Notes](https://developer.apple.com/library/mac/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226-CH1-SW1) says "`__bridge` transfers a pointer between Objective-C and Core Foundation with no transfer of ownership." – Rob Dec 31 '13 at 20:16

1 Answers1

5

It's part of Automatic Reference Counting (ARC). From the docs:

__bridge transfers a pointer between Objective-C and Core Foundation with no transfer of ownership.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91