Changes to NSTextView not reaching screen
I have an NSTextView backed by a text system I put together myself, along the lines of the "Assembling the Text System by Hand" section in the Text System Overview in the Cocoa documentation. It successfully displays the contents of the NSTextStorage on the screen.
But when I type into it, nothing seems to happen—the text on screen doesn't change. If I select the text, the shape of the selection suggests that 开发者_StackOverflowthe text has changed. If I copy and paste the text into TextEdit, I can even see my edits. And if I type enough, I can get it to throw an exception:
NSRunStorage, _NSBlockNumberForIndex(): index (5897) beyond array bounds (5881)
Golly, this looks like the kind of thing I could spend the better part of a day debugging. What's going on here?
Make sure you connected the NSTextStorage to the NSLayoutManager like this:
[textStorage addLayoutManager:layoutManager];
Instead of like this:
[layoutManager setTextStorage:textStorage]; //BAD
The documentation on -[NSLayoutManager setTextStorage:]
says that it should never be called directly, only overridden. Apparently this is the reason. Hopefully I saved you hours and hours of fruitless work!
精彩评论