It crashes when trying to set the textView content
pepicrft opened this issue · comments
When I try to set the content the application crashes. This is how I set it:
internal func setBody(body: String) {
let attributed = NSAttributedString(string: body)
self.bodyTextView.attributedText = attributed
self.textStorage.appendAttributedString(attributed)
}
Before I've initialized the MarklightTextStorage and add the layout manager. This is the crash I got:
2016-05-21 10:49:24.892 Gitdo[49591:2764396] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSCFConstantString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range {0, 53} out of bounds; string length 0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010e85cf45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000011386ddeb objc_exception_throw + 48
2 CoreFoundation 0x000000010e85ce7d +[NSException raise:format:] + 205
3 Foundation 0x000000010f37c977 -[NSString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:] + 229
4 Foundation 0x000000010f37c88b -[NSString getParagraphStart:end:contentsEnd:forRange:] + 41
5 UIFoundation 0x0000000114d2cade _NSFastFillAllLayoutHolesForGlyphRange + 1266
6 UIFoundation 0x0000000114d2a14e -[NSLayoutManager(NSPrivate) _rectArrayForRange:withinSelectionRange:rangeIsCharRange:singleRectOnly:fullLineRectsOnly:inTextContainer:rectCount:rangeWithinContainer:glyphsDrawOutsideLines:rectArray:rectArrayCapacity:] + 822
7 UIFoundation 0x0000000114d2d5bc -[NSLayoutManager(NSPrivate) _rectArrayForRange:withinSelectionRange:rangeIsCharRange:singleRectOnly:fullLineRectsOnly:inTextContainer:rectCount:rangeWithinContainer:glyphsDrawOutsideLines:] + 117
8 UIFoundation 0x0000000114d65fa0 -[NSLayoutManager rectArrayForCharacterRange:withinSelectedCharacterRange:inTextContainer:rectCount:] + 80
9 UIKit 0x000000011250669d -[_UITextContainerView updateInsertionPointStateAndRestartTimer:] + 311
10 UIFoundation 0x0000000114d31ee3 -[NSLayoutManager(NSPrivate) _invalidateLayoutForExtendedCharacterRange:isSoft:invalidateUsage:] + 2118
11 UIFoundation 0x0000000114d61fea -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 219
12 UIFoundation 0x0000000114d622d2 -[NSLayoutManager processEditingForTextStorage:edited:range:changeInLength:invalidatedRange:] + 47
13 UIFoundation 0x0000000114d89a76 -[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 152
14 UIFoundation 0x0000000114d89591 -[NSTextStorage processEditing] + 349
15 UIFoundation 0x0000000114d891eb -[NSTextStorage endEditing] + 82
16 UIKit 0x00000001124eff2c -[UITextView setAttributedText:] + 250
17 Gitdo 0x000000010c87a842 _TFC5Gitdo10EditorView7setBodyfSST_ + 146
18 Gitdo 0x000000010c8072ea _TFC5Gitdo15EditorPresenter9viewReadyfT_T_ + 170
19 Gitdo 0x000000010c87a5e5 _TFC5Gitdo10EditorView11viewDidLoadfT_T_ + 613
20 Gitdo 0x000000010c87a792 _TToFC5Gitdo10EditorView11viewDidLoadfT_T_ + 34
21 UIKit 0x0000000111ceccc4 -[UIViewController loadViewIfRequired] + 1198
22 UIKit 0x0000000111cf2c7b -[UIViewController __viewWillAppear:] + 120
23 UIKit 0x0000000111d22a37 -[UINavigationController _startCustomTransition:] + 1203
24 UIKit 0x0000000111d32cdb -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
25 UIKit 0x0000000111d33cea -[UINavigationController __viewWillLayoutSubviews] + 57
26 UIKit 0x0000000111ed9c85 -[UILayoutContainerView layoutSubviews] + 248
27 UIKit 0x0000000111c0ee40 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
28 QuartzCore 0x000000011054259a -[CALayer layoutSublayers] + 146
29 QuartzCore 0x0000000110536e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
30 QuartzCore 0x0000000110536cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
31 QuartzCore 0x000000011052b475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
32 QuartzCore 0x0000000110558c0a _ZN2CA11Transaction6commitEv + 486
33 UIKit 0x0000000111b54ca4 _UIApplicationHandleEventQueue + 7329
34 CoreFoundation 0x000000010e789011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
35 CoreFoundation 0x000000010e77ef3c __CFRunLoopDoSources0 + 556
36 CoreFoundation 0x000000010e77e3f3 __CFRunLoopRun + 867
37 CoreFoundation 0x000000010e77de08 CFRunLoopRunSpecific + 488
38 GraphicsServices 0x000000011595ead2 GSEventRunModal + 161
39 UIKit 0x0000000111b5a30d UIApplicationMain + 171
40 Gitdo 0x000000010c7c8a72 main + 114
41 libdyld.dylib 0x00000001149b892d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Am I doing anything wrong? Thanks!
Hi @pepibumur
in the sample project I've assigned the layoutManager
after setting the attributed string to avoid the crash.
I'm still going to try to solve the issue, but for now can you to switch the rows?
// Load a sample markdown content from a file inside the app bundle
if let samplePath = NSBundle.mainBundle().pathForResource("Sample", ofType: "md"){
do {
let string = try String(contentsOfFile: samplePath)
// Convert string to an `NSAttributedString`
let attributedString = NSAttributedString(string: string)
// Set the loaded string to the `UITextView`
textView.attributedText = attributedString
// Append the loaded string to the `NSTextStorage`
textStorage.appendAttributedString(attributedString)
} catch _ {
print("Cannot read Sample.md file")
}
}
// Assign the `UITextView`'s `NSLayoutManager` to the `NSTextStorage` subclass
textStorage.addLayoutManager(textView.layoutManager)
It works @macteo , thanks a lot :P
Thanks for your feedback!
BTW great app and interface.
thanks @macteo
I think setting the attributedText
or text
directly on a UITextView
is actually meaningless when you're providing your own NSTextStorage
, since the text view no longer owns its own storage. If you never access attributedText
/text
directly, but instead do textStorage.setAttributedString(myAttributedString)
, everything should work fine.
(At least, it works for me. Correct me if I'm wrong about this!)
@pepibumur random question, but do you happen to be using RFKeyboardToolbar?
I'm closing it based on @sadlerjw consideration.