문제

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 the 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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top