问题
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 theNSInputManager
class andNSTextInput
protocol specifications for more information. ...
In NSTextInput
there is a note:
IMPORTANT
NSTextInput
protocol is slated for deprecation. Please useNSTextInputClient
protocol, introduced in OS X v10.5, as described inNSTextInputClient 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