xcode (NSCFType set) - iOS 6 中无法识别的选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12533623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:33:41  来源:igfitidea点击:

(NSCFType set) - Unrecognized selector in iOS 6

iosxcodeios6core-textctfontref

提问by rwbutler

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:

我正在使用很棒的 TTTAttributedLabel ( https://github.com/mattt/TTTAttributedLabel),它在 iOS 5 下运行良好。但是,在 iOS 6 下,我收到错误消息:

-[__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'd 0x200020e0 它似乎是一个 CTFontRef。

po 0x200020e0
(int) 
 [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;
}];
= 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:

这让我直接看到了设置 TTTAttributedLabel 的代码:

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

as in the example usage here:

如此处的示例用法:

https://github.com/mattt/TTTAttributedLabel

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).

该代码不是 ARCified,所以我添加了桥接转换(见上文)。我已经尝试在所有地方保留,但这似乎并没有解决 CTFontRef 过早发布的问题(似乎是)(我认为 - 欢迎其他建议)。

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

关于如何解决这个问题以及为什么这只会在 iOS 6 模拟器下出现的任何想法?提前致谢。

采纳答案by rwbutler

In the end it was just a stupid mistake -

最后只是一个愚蠢的错误——

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

should have read:

应该读过:

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

回答by ??u??

Replaced:

替换:

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

with:

和:

##代码##

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

为我修复了 iOS6 中的问题。未在 iOS 5 上测试。

回答by chongsj

I encountered the same error when I have a UILabel myLabel, and doing myLabel.attributedText = NSMutableAttributedString.

当我有一个 UILabel myLabel 并执行 myLabel.attributedText = NSMutableAttributedString 时,我遇到了同样的错误。

Try https://github.com/AliSoftware/OHAttributedLabel

试试https://github.com/AliSoftware/OHAttributedLabel

Declaring:

声明:

@property (nonatomic, strong) IBOutlet OHAttributedLabel *myLabel;

@property (nonatomic, strong) IBOutlet OHAttributedLabel *myLabel;

instead of UILabel solves the error.

而不是 UILabel 解决了错误。

回答by CMash

I managed to get this error when I was using a bad range adding attributes to an NSMutableAttributedString. Fixing the range fixed the crash!

当我使用错误的范围向 NSMutableAttributedString 添加属性时,我设法得到了这个错误。修复范围修复了崩溃!

EDIT: Also managed to get it by using UITextAttributeFont in the attributes dictionary which is apparently now deprecated in iOS 7 which just makes things crash mysteriously. The replacement is NSForegroundColorAttributeName.

编辑:还设法通过在属性字典中使用 UITextAttributeFont 来获得它,该字典现在显然已在 iOS 7 中弃用,这只会使事情神秘地崩溃。替换是 NSForegroundColorAttributeName。