UITextView / NSAttributedString:iOS 错误?

UITextView / NSAttributedString: iOS Bug?

单击我在 UITextView 中创建的自定义 link 时出现以下崩溃错误。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString scheme]: unrecognized selector sent to instance 0x10e7aa098'

这就是我的代码的样子。

 NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: @"20151230copy.png"];
 NSRange range = NSMakeRange(0, [attrString length]);

 [attrString beginEditing];
 [attrString addAttribute:NSLinkAttributeName value:@"https://www.youtube.com/" range:range];
 [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
 [attrString addAttribute:
    NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:range];
   [attrString endEditing];

 self.textView.attributedText = attrString;

我看不出我做错了什么,我认为这可能是一个 iOS 错误。如果我将 20151230copy 替换为 "texas" 之类的东西,它就可以正常工作。这对我来说毫无意义。 亲自尝试一下,您就会明白我的意思。

我认为这里发生的事情是 NSLinkAttributevalue 应该是 NSURL,而不是字符串常量。

错误中的通知 (... [__NSCFConstantString scheme] ...) 它正在尝试将 NSURL selector 发送到常量字符串。

苹果文档: NSLink属性名 此属性的值是 NSURL 对象(首选)或 NSString 对象。这个属性的默认值为nil,表示没有link.

在 iOS 7.0 及更高版本中可用。