rwbutler rwbutler - 1 year ago 117
iOS Question

(NSCFType set) - Unrecognized selector in iOS 6

I'm using the great TTTAttributedLabel ( which works fine under iOS 5. Under iOS 6 however, I get the error:

-[__NSCFType set]: unrecognized selector sent to instance 0x200020e0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [__NSCFType set]: unrecognized selector sent to instance 0x200020e0'

Having researched the issue a little, it would appear that the set message is being sent to an object which has been released. Using the debugger, I have po'd 0x200020e0 which appears to be a CTFontRef.

po 0x200020e0
(int) $0 = 536879328 CTFont <name: .HelveticaNeueUI-Bold, size: 20.000000, matrix: 0x0>
CTFontDescriptor <attributes: <CFBasicHash 0x20001900 [0x3c2a4100]>{type = mutable dict, count = 1,
entries =>
1 : <CFString 0x3be2a768 [0x3c2a4100]>{contents = "NSFontNameAttribute"} = <CFString 0x3c292c14 [0x3c2a4100]>{contents = ".HelveticaNeueUI-Bold"}


This led me straightaway to the code which sets up the TTTAttributedLabel:

[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSRange boldRange = [[mutableAttributedString string] rangeOfString:title options:NSCaseInsensitiveSearch];
NSRange strikeRange = [[mutableAttributedString string] rangeOfString:@"sit amet" options:NSCaseInsensitiveSearch];

UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:20];

CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);

if (font) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(__bridge id)font range:boldRange];

return mutableAttributedString;

as in the example usage here:

That code isn't ARCified so I've added in bridged casts (see above). I've tried retains all over the place but that doesn't seem to solve the issue (which appears to be) that the CTFontRef is getting released too early (I think - other suggestions welcome).

Any ideas on how to solve this and why this only crops up under the iOS 6 simulator? Thanks in advance.

Answer Source

In the end it was just a stupid mistake -

[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(__bridge id)font range:boldRange];

should have read:

[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:boldRange];