NSTextView's insertText method is deprecated in OS X v10.11. What is the replacement?

独自空忆成欢 提交于 2019-12-07 09:30:19

问题


I saw in the AppKit API Reference that the insertText method is deprecated in OS X v10.11. What am I supposed to use as a replacement?


回答1:


The documentation says

- (void)insertText:(id)aString

This method is the means by which text typed by the user enters an NSTextView. See the NSInputManager class and NSTextInput protocol specifications for more information. ...

In NSTextInput there is a note:

IMPORTANT

NSTextInput protocol is slated for deprecation. Please use NSTextInputClient protocol, introduced in OS X v10.5, as described in NSTextInputClient Protocol Reference.

In NSTextInputClient protocol there is a method

- (void)insertText:(id)aString
  replacementRange:(NSRange)replacementRange

This seems to be the appropriate replacement




回答2:


Old method

- (void)insertText:(id)aString

New method

- (void)insertText:(id)aString replacementRange:(NSRange)replacementRange

Using the new insertText method in practice

// Start String: abcdef

// Result: a++bcdef
[NSTextView insertText:@"++" replacementRange:NSMakeRange(1, 0)];

// Result: a++def
[NSTextView insertText:@"++" replacementRange:NSMakeRange(1, 2)];

// Result: a++f
[NSTextView insertText:@"++" replacementRange:NSMakeRange(1, 4)];


来源:https://stackoverflow.com/questions/32787814/nstextviews-inserttext-method-is-deprecated-in-os-x-v10-11-what-is-the-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!