我有以下快速代码来生成属性字符串,
func getAttributedTermsAndServicesText() -> NSAttributedString {
let text = StringConstants.RegisterConstants.termsOfUseLabelText
let attributedString = NSMutableAttributedString(string: text)
let termsRange = attributedString.mutableString.range(of: StringConstants.CommonConstants.termsOfUse)
attributedString.addAttribute(NSAttributedStringKey.font, value: WebViewLinkType.termsOfUse.getLink(), range: termsRange)
attributedString.addAttribute(NSAttributedStringKey.font, value: NSUnderlineStyle.styleSingle.rawValue, range: termsRange)
let privacyRange = attributedString.mutableString.range(of: StringConstants.CommonConstants.privacyStatement)
attributedString.addAttribute(NSAttributedStringKey.link, value: WebViewLinkType.privacyStatement.getLink(), range: privacyRange)
attributedString.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: privacyRange)
print(attributedString.mutableString)
return attributedString
}
此方法返回生成的属性字符串。但是在这一行代码上崩溃
//Generate attributed string
let attributedString = getAttributedTermsAndServicesText()
//crash after this line
cell.textView.attributedText = attributedString
它在控制台中打印以下详细信息,
-[__NSCFNumber _isDefaultFace]:发送到实例 0x15d0d9c0 ( "4 libc++abi.dylib 0x1ca0798f + 78", "5 libc++abi.dylib 0x1ca07493 __cxa_rethrow + 90", "6 利比亚。A.迪利布 0x1ca131ab objc_exception_rethrow + 42", "7 核心基金会 0x1d699231 CFRunLoopRunSpecific + 600", "8 核心基金会 0x1d698fd1 CFRunLoopRunInMode + 104" ( 2018-06-06 02:52:07.439226-0700 MyApp[370:63347]* 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException', reason: '-[__NSCFNumber _isDefaultFace]: 无法识别的选择器发送到实例 0x15d0d9c0" *第一个抛出调用堆栈:(0x1d78bb3d 0x1ca13067 0x1d790fd1 0x1d78f0c3 0x1d6b7dc8 0x2292fe93 0x2292f727 0x2294bf4f 0x2294bc43 0x22a66523 0x1cc728 0x1c42c0 0x1cd0f0 0x22cebc13 0x22cebde5 0x22cd9d79 0x22cf08db 0x22a9cf0f 0x229bb483 0x20613cb7 0x20607d77 0x20607c05 0x20596839 0x205b4fdb 0x205248e3 0x1f9c31bf 0x1d9dca7f 0x1d734c19 0x1d74865f 0x1d747ebd 0x1d745eab 0x1d6991af 0x1d698fd1 0x1ee43b41 0x22a21a53 0x28d578 0x1ce864eb( libc++abi.dylib:结尾为 NSException 类型的未捕获异常
但是,我仍然无法获得这次崩溃的确切原因。 以前有人遇到过这种问题吗?
请添加字体名称和大小
func getAttributedTermsAndServicesText() -> NSAttributedString {
let text = StringConstants.RegisterConstants.termsOfUseLabelText
let attributedString = NSMutableAttributedString(string: text)
let termsRange = attributedString.mutableString.range(of: StringConstants.CommonConstants.termsOfUse)
attributedString.addAttribute(NSAttributedStringKey.link, value: WebViewLinkType.termsOfUse.getLink(), range: termsRange)
attributedString.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: termsRange)
let privacyRange = attributedString.mutableString.range(of: StringConstants.CommonConstants.privacyStatement)
attributedString.addAttribute(NSAttributedStringKey.link, value: WebViewLinkType.privacyStatement.getLink(), range: privacyRange)
attributedString.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: privacyRange)
print(attributedString.mutableString)
return attributedString
}