Re: __bridge cast on CF object stored in NSDictionary?
Re: __bridge cast on CF object stored in NSDictionary?
- Subject: Re: __bridge cast on CF object stored in NSDictionary?
- From: "Clark S. Cox III" <email@hidden>
- Date: Tue, 13 Dec 2011 19:53:12 -0800
On Dec 13, 2011, at 6:40 PM, Rick Mann wrote:
> I've read the docs a couple times, but still am not sure what's going on in this instance:
>
> CTFontRef f = CTFontCreateWithName((__bridge CFStringRef) name, inFont.pointSize, NULL);
> [self.currentAttrs setValue: (id) f forKey: (NSString*) kCTFontAttributeName];
> CFRelease(f);
>
> self.currentAttrs is an NSMutableDictionary. The CTFontRef f is generating an error when passed to setValue:
>
> Cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast
That error is actually being generated by the "(NSString*) kCTFontAttributeName" cast
> What type of __bridge cast should I use here?
For the "(NSString*) kCTFontAttributeName", you should do "(__bridge NSString*) kCTFontAttributeName". (The plain __bridge doesn't retain or release anything, so it's appropriate for constants such as "kCTFontAttributeName")
For the "(id)f", you can do one of:
1)
CTFontRef f = CTFontCreateWithName((__bridge CFStringRef) name, inFont.pointSize, NULL);
[self.currentAttrs setValue: CFBridgingRelease(f) forKey: (__bridge NSString*) kCTFontAttributeName];
//No CFRelease(f)
2)
CTFontRef f = CTFontCreateWithName((__bridge CFStringRef) name, inFont.pointSize, NULL);
[self.currentAttrs setValue: (__bridge_transfer id)f forKey: (__bridge NSString*) kCTFontAttributeName];
//No CFRelease(f)
3)
CTFontRef f = CTFontCreateWithName((__bridge CFStringRef) name, inFont.pointSize, NULL);
[self.currentAttrs setValue: (__bridge id)f forKey: (__bridge NSString*) kCTFontAttributeName];
CFRelease(f);
"CFBridgingRelease(foo)" and "(__bridge_transfer id)foo" are functionally equivalent, but I prefer the CFBridgingRetain/CFBridgingRelease functions wherever possible as they make it clear what is actually happening (i.e. CFBridgingRelease is conceptually a CFRelease combined with transferring to ARC, while CFBridgingRetain is conceptually a CFRetain combined with transferring from ARC).
--
Clark S. Cox III
email@hidden
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden