textViewDidChange: crashes in iOS 7

What's happening is that you're typing what is referred to as multistage text input, i.e. the input has to be confirmed from the user before it's actually committed into the underlying text. You get the crash because the text is only kind-of inputted, and until the text has been committed, it could easily change to something else.

To detect this situation, you're looking for the markedTextRange property of the UITextView, which indicates if you're in this complex input mode.

If the property is non-nil, then you're in this special input mode, so you should guard your modification code with something like:

if (self.tv.markedTextRange == nil && self.tv.text.length > maxLength) {
    // Perform change
}

Near as I can tell the crash is triggered by the special multistage text input mode, so you should avoid changing the text while this mode is active.

原文地址:https://www.cnblogs.com/ygm900/p/4819784.html