Udi*_*wal 8 unicode objective-c uitextview ios
我试图设置UITextView其中有一些非法字符的文本,如"Unicode字符'OBJECT REPLACEMENT CHARACTER'(U + FFFC)".
基本上,我有一个UITextView.现在用户点击它并且键盘出现.现在我使用语音从键盘发送文本(也称为听写).当听写处理时(此时UITextView具有对应于默认占位符动画的特殊字符),我尝试使用以下方法设置文本视图的值:
textView.text = @""
Run Code Online (Sandbox Code Playgroud)
这会造成以下崩溃:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString replaceCharactersInRange:withString:]: Range or index out of bounds'
Run Code Online (Sandbox Code Playgroud)
我从崩解剂中获得的堆栈跟踪:
0 CoreFoundation __exceptionPreprocess + 130
2 CoreFoundation -[NSException initWithCoder:]
3 CoreFoundation mutateError + 222
4 Foundation -[NSString stringByReplacingCharactersInRange:withString:] + 134
5 UIKit __37-[UITextInputController textInRange:]_block_invoke + 310
6 UIFoundation -[NSTextStorage coordinateReading:] + 36
7 UIKit -[UITextInputController textInRange:] + 232
8 UIKit -[TIDocumentState(UITextInputAdditions) _contextAfterPosition:inDocument:] + 190
9 UIKit -[TIDocumentState(UITextInputAdditions) initWithDocument:] + 150
10 UIKit +[TIDocumentState(UITextInputAdditions) documentStateOfDocument:] + 52
11 UIKit -[UIKeyboardImpl updateForChangedSelectionWithExecutionContext:] + 288
12 UIKit -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 352
13 UIKit -[UIKeyboardTaskQueue performTask:] + 248
14 UIKit -[UIKeyboardImpl updateForChangedSelection] + 96
15 UIKit -[UIKeyboardImpl selectionDidChange:] + 102
16 UIFoundation -[NSTextStorage coordinateReading:] + 36
17 UIKit -[UITextInputController _coordinateSelectionChange:] + 100
18 UIKit -[UITextInputController _setSelectedTextRange:] + 604
19 UIKit -[UITextView setAttributedText:] + 392
20 UIKit -[UITextView setText:] + 134
Run Code Online (Sandbox Code Playgroud)
我还创建了一个演示此问题的示例项目.您可以从以下网址获取此项目:https://dl.dropboxusercontent.com/u/80141854/TextViewDictationCheck.zip
可通过以下步骤复制例外:
我找到了一种避免崩溃的方法:在设置UITextView的文本时,我们可以使用以下代码:
我还发现了一个在UITextView上重置文本时可以使用的解决方法:
[self.textView setSelectedRange:NSMakeRange(0, [[self.textView textStorage] length])];
[self.textView insertText:@""];
[self.textView setText:@""];
Run Code Online (Sandbox Code Playgroud)
但是,如果我们只使用setText:来设置文本,我仍然没有理解这种崩溃的原因.
异常发生在基础代码中,而不是您的代码中。如果在处理听写时更改字符串,则存在竞争条件,导致此崩溃。当您触摸麦克风按钮时,它会在字符串中放置一个占位符,然后在处理完成后将其替换为文本。如果更改字符串并删除占位符,则会导致崩溃。
解决方法是确保在处理听写时不会更改字符串。您可以通过检查当前输入模式的主要语言来执行此操作。dictation当听写正在进行时它被设置为:
- (IBAction)sendPressed:(id)sender
{
NSString *primaryLanguage = [self.textView textInputMode].primaryLanguage;
if(![primaryLanguage isEqualToString:@"dictation"])
{
// Your original method body:
NSString *textViewText = self.textView.text;
textViewText = @"";
self.textView.text = nil;
self.textView.text = @"";
}
Run Code Online (Sandbox Code Playgroud)
}
如果正在进行听写,这将跳过代码以清空文本视图。
| 归档时间: |
|
| 查看次数: |
3203 次 |
| 最近记录: |