Objective c 面临崩溃以检查NSMutableAttributeString中的特殊字符

Objective c 面临崩溃以检查NSMutableAttributeString中的特殊字符,objective-c,iphone,nsstring,nsmutableattributedstring,Objective C,Iphone,Nsstring,Nsmutableattributedstring,我有一个NSString存储在cell.lblTitle.text中。我正在使用以下代码将此NSString转换为NSMutableAttributedString text = [[NSMutableAttributedString alloc] initWithData:[cell.lblTitle.text dataUsingEncoding:NSUTF8StringEncoding] optio

我有一个
NSString
存储在
cell.lblTitle.text
中。我正在使用以下代码将此
NSString
转换为
NSMutableAttributedString

text = [[NSMutableAttributedString alloc] initWithData:[cell.lblTitle.text dataUsingEncoding:NSUTF8StringEncoding]
                                               options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                    documentAttributes:nil error:nil];

[text setAttributes:@{NSFontAttributeName:labelFont} range:NSMakeRange(0, [text length])];
[text addAttribute:NSForegroundColorAttributeName
             value:[UIColor darkGrayColor]
             range:NSMakeRange(0, [text length])];
当我在控制台中打印cell.lblTitle.text及其长度时,以下是我的输出:

po cell.lblTitle.text

Address: HM<MM-
UU@FF-
Mobile,
Alabama,
123456-
United States

po [cell.lblTitle.text length]
61
以下是我的崩溃日志:

***由于未捕获的异常“NSRangeException”而终止应用程序,原因:“NSMutableRLEARY objectAtIndex:effectiveRange::Out of of” 边界'


因此,特殊字符“文本后的字符串不是html,因此不要尝试将其创建为html。只需使用
initWithString:
直接从文本创建属性字符串。

这是()…?或不是。这不是实际的标记。在本例中,它被视为特殊字符。请添加崩溃日志?请参阅更新的崩溃日志答案[text length]-1放置并检查,您遇到了哪一行崩溃?
po text
Address: HM{
    NSColor = "UIDeviceWhiteColorSpace 0.333333 1";
    NSFont = "<UICTFont: 0x7aebc0f0> font-family: \"Futura-Medium\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
}

po [text length]
11