I’m using an existing library that wasn’t compiled with ARC and had the following line of code that I need to convert over when I upgraded my project:
CFAttributedStringSetAttribute(text,
CFRangeMake(position, length),
kCTStrokeWidthAttributeName,
[NSNumber numberWithFloat:[[attributes objectForKey:@"stroke"] intValue]]);
Compiling with ARC of course produced a few errors and I modified the code to make it more compliant. Is the code change below correct?
int strokeInt = [[attributes objectForKey:@"stroke"] intValue];
CFNumberRef strokeNumber = CFNumberCreate(NULL, kCFNumberSInt32Type, &strokeInt);
CFAttributedStringSetAttribute(text,
CFRangeMake(position, length),
kCTStrokeWidthAttributeName,
strokeNumber);
Any suggestions would be appreciated.
You’re leaking
strokeNumberunless you are callingCFReleaseon it. ButCFNumberis toll-free bridged withNSNumber, so you can eliminatestrokeNumberand just use a cast:Furthermore, if
[attributes objectForKey:@"stroke"]happens to already be anNSNumber, you can simplify to just this: