App crashes when I try to convert HTML string to NSAttributedString and assign it to a textView.
@IBOutlet weak var standingBillerNoteTextView: UITextView!
self.standingBillerNoteTextView.attributedText = isFlexSip ? paymentVM.flexSipPaymentNotes.htmlToAttributedString: paymentVM.sipSINotes.htmlToAttributedString
I am using below code for conversion -
extension String {
var htmlToAtrributtedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return NSAttributedString() }
do {
//**Crash is in below line**
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
debugPrint(error.localizedDescription)
return nil
}
}
}
PFB the crash report -
# Crashlytics - Stack trace
# Platform: apple
# Date: Fri Dec 08 2023 20:45:06 GMT+0530 (India Standard Time)
Fatal Exception: NSInternalInconsistencyException
0 CoreFoundation 0xed5e0 __exceptionPreprocess
1 libobjc.A.dylib 0x2bc00 objc_exception_throw
2 Foundation 0x6bc4d4 -[NSMutableDictionary(NSMutableDictionary) initWithContentsOfFile:]
3 UIKitCore 0x297f80 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
4 CoreFoundation 0x361dc __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
5 CoreFoundation 0x34be4 __CFRunLoopDoObservers
6 CoreFoundation 0x33e64 CFRunLoopRunSpecific
7 UIFoundation 0xdace8 -[NSHTMLReader _loadUsingWebKit]
8 UIFoundation 0xdb898 -[NSHTMLReader attributedString]
9 UIFoundation 0x84128 _NSReadAttributedStringFromURLOrDataCommon
10 UIFoundation 0x7f2b8 _NSReadAttributedStringFromURLOrData
11 UIFoundation 0x7f130 -[NSAttributedString(NSAttributedStringUIFoundationAdditions) initWithData:options:documentAttributes:error:]
12 MyProject 0x3bbbb0 @nonobjc NSAttributedString.init(data:options:documentAttributes:) (<compiler-generated>)
13 MyProject 0x3bb6ec NSAttributedString.init(data:options:documentAttributes:) (<compiler-generated>)
14 MyProject 0x3bb3dc String.htmlToAttributedString.getter + 96 (StringExtension.swift:96)
15 MyProject 0x52e538 TransactionPaymentTableViewCell.setUpNotes() + 240 (TransactionPaymentTableViewCell.swift:240)
16 MyProject 0x52b670 TransactionPaymentTableViewCell.configureCell(type:folioNumber:cellViewModel:frequencyType:schemeType:contactMissing:considering:dynamicDataVM:) + 135 (TransactionPaymentTableViewCell.swift:135)
17 MyProject 0x63835c SIPSchemeViewController.tableView(_:cellForRowAt:) + 531 (SIPSchemeViewController.swift:531)
18 MyProject 0x641a98 @objc SIPSchemeViewController.tableView(_:cellForRowAt:) (<compiler-generated>)
19 UIKitCore 0x28a08c (Missing UUID 948b271ad49d3b3fac082223908f51ab)
20 UIKitCore 0x289ac4 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
21 UIKitCore 0x288010 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
22 UIKitCore 0x287af8 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
23 UIKitCore 0x331d8 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
24 QuartzCore 0x67780 (Missing UUID 0fb9e837c0aa3977a0be28ba0f207a50)
25 QuartzCore 0x67308 (Missing UUID 0fb9e837c0aa3977a0be28ba0f207a50)
26 QuartzCore 0x6d844 (Missing UUID 0fb9e837c0aa3977a0be28ba0f207a50)
27 QuartzCore 0x66b34 (Missing UUID 0fb9e837c0aa3977a0be28ba0f207a50)
28 UIKitCore 0xe53790 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
29 UIKitCore 0xe01a3c (Missing UUID 948b271ad49d3b3fac082223908f51ab)
30 UIKitCore 0xe51620 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
31 UIKitCore 0xe51a88 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
32 UIKitCore 0x122adb4 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
33 UIKitCore 0xbe3ff4 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
34 UIKitCore 0x79ebbc (Missing UUID 948b271ad49d3b3fac082223908f51ab)
35 UIKitCore 0x79eef0 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
36 UIKitCore 0x79e5fc (Missing UUID 948b271ad49d3b3fac082223908f51ab)
37 UIKitCore 0x20dad0 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
38 UIKitCore 0xc86fdc (Missing UUID 948b271ad49d3b3fac082223908f51ab)
39 UIKitCore 0x2d24cc (Missing UUID 948b271ad49d3b3fac082223908f51ab)
40 UIKitCore 0x2cfce4 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
41 UIKitCore 0x2cf418 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
42 UIKitCore 0x2ceb60 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
43 UIKitCore 0x10052c (Missing UUID 948b271ad49d3b3fac082223908f51ab)
44 UIKitCore 0xffa6c (Missing UUID 948b271ad49d3b3fac082223908f51ab)
45 UIKit 0xa3b8 -[UIKeyboardSceneDelegateAccessibility setInputViews:animationStyle:]
46 UIKitCore 0xff9b8 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
47 UIKitCore 0x136534 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
48 UIKitCore 0x8a96ec (Missing UUID 948b271ad49d3b3fac082223908f51ab)
49 UIKitCore 0xc5a94c (Missing UUID 948b271ad49d3b3fac082223908f51ab)
50 libdispatch.dylib 0x4300 _dispatch_client_callout
51 libdispatch.dylib 0x77b8 _dispatch_continuation_pop
52 libdispatch.dylib 0x1b5c0 _dispatch_source_latch_and_call
53 libdispatch.dylib 0x1a190 _dispatch_source_invoke
54 libdispatch.dylib 0x128a8 _dispatch_main_queue_drain
55 libdispatch.dylib 0x125b0 _dispatch_main_queue_callback_4CF
56 CoreFoundation 0x379bc __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
57 CoreFoundation 0x346c8 __CFRunLoopRun
58 CoreFoundation 0x33e18 CFRunLoopRunSpecific
59 GraphicsServices 0x35ec GSEventRunModal
60 UIKitCore 0x22f350 (Missing UUID 948b271ad49d3b3fac082223908f51ab)
61 UIKitCore 0x22e98c (Missing UUID 948b271ad49d3b3fac082223908f51ab)
62 MyProject 0xfbf9ec main + 20 (AppDelegate.swift:20)
63 ??? 0x1d2d47d44 (Missing)
I have seen many posts in stackOverflow, Medium, Apple developer, etc but did not find any solution. It occurs only in some devices and very random. Please provide a solution. Thank you in advance.