0

I've implemented a simple drfat storage for UITextView I use in application. Everything works fine, but when I retrieve this draft, assign it to the UITextView and set selectedRange (to also some stored value) I get this weird warning:

requesting caretRectForPosition: with a position beyond the NSTextStorage (4)

From the things I've noticed... This only happens for strings which are shorter than 4 characters (this may give some clue, because 4 also appears in the warning message).
It may be helpful ti post the actual assignment code, so here it is:

self.textView.text = savedDraft.text;
self.textView.selectedRange = NSMakeRange(savedDraft.cursorPosition, 0);

Do you have any idea what can generate this kind of warning? I've found here that this may be caused by Swift implementation of UITextView, but we're pure Objective-C.


Update:

When I save I do save this location as NSUInteger and I get it from my UITextView like this:

self.textView.selectedRange.location

So if you consider a simple "Hi" message (two characters) I get this kind of logs form save and retrieve methods:

Get draft position: 0
Saving draft position: 2
Get draft position: 2
cojoj
  • 5,965
  • 4
  • 28
  • 48

1 Answers1

0

It looks like UIKeyboardWillShowNotification is playing the big role here and when everywhing happens in viewDidLoad it creates conflicts. It seems that keyboard showing also affects caret position and when I manually change self.textView.selectedRange I do it too fast, so it conflicts with this keyboard notifiaction.

For me the solution was to simply put this selectedRange thing in block with some really small delay like this:

@weakify(self)
[JDLDispatchUtils asyncOnMainAfter:0.01 block:^{
    @strongify(self)
    self.textView.text = savedDraft.text;
    self.textView.selectedRange = NSMakeRange(savedDraft.cursorPosition, 0);
}];
cojoj
  • 5,965
  • 4
  • 28
  • 48