(NSCFType set) - Unrecognized selector in iOS 6

rwbutler picture rwbutler · Sep 21, 2012 · Viewed 12.1k times · Source

I'm using the great TTTAttributedLabel (https://github.com/mattt/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];
        CFRelease(font);
    }

    return mutableAttributedString;
}];

as in the example usage here:

https://github.com/mattt/TTTAttributedLabel

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

ıɾuǝʞ picture ıɾuǝʞ · Mar 28, 2013

Replaced:

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

with:

UIFont *font = [UIFont fontWithName:boldSystemFont.fontName size:boldSystemFont.pointSize];

Fixed the issue in iOS6 for me. Not tested on iOS 5.