Reputation: 33080
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
NSRange rangeHighlight = NSMakeRange(range.location, substringToHighlight.length);
if (font) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:rangeHighlight];
CFRelease(font); //Is this still necessary?
}
I copy and paste this code from https://github.com/mattt/TTTAttributedLabel
CTFontRef font = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (font) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)font range:boldRange];
[mutableAttributedString addAttribute:@"TTTStrikeOutAttribute" value:[NSNumber numberWithBool:YES] range:strikeRange];
CFRelease(font);
}
When I do that I got an error saying that I got to use the keyword __bridge. What is it? I put it and compile error stop. But then I wonder if I still need to use CFRelease(font)
In addition
Upvotes: 2
Views: 859
Reputation: 56625
Yes you still need to use CFRelease(font).
You are still the one creating the font so you need to release it as well. The __bridge part is related to how the font name is retained or not.
CF is short for Core Foundation, the C level API that Foundation is built upon. CFRelease is how you release a Core Foundation object.
__bridge tells ARC how it should retain or not retain an objects when translating it to a Core Foundation object. This question explains the different __bridge types.
You should still release (explained above).
Search for "Core Foundation". The Design Concepts explains the general design.
Upvotes: 2
Reputation: 95335
CF stands for "Core Foundation". A CTFontRef is a Core Foundation type so you can release it with CFRelease().
__bridge
is a keyword used when casting from a retainable type to a non-retainable type (or vice versa) to tell the compiler there should be no change of ownership happening.
Yes, because __bridge
does not change who the owner is.
This documentation is fairly comprehensive.
Upvotes: 2