Note that there are some explanatory texts on larger screens.

plurals
  1. POtextView:shouldChangeTextInRanges:replacementStrings: method crashes when NSTextStorage is edited
    primarykey
    data
    text
    <p>I'm new to Cocoa programming under OSX Lion, so this is a question I cannot find answer to. I need to modify an attributed string which is entered by the user through the <code>NSTextView</code>. Editing may include replacement of some portion(s) of the string inside the <code>NSTextStorage</code> and addition of <code>NSTextAttachment</code> objects. I implemented the protocol and provided it for <code>NSTextView</code> instance as delegate. I also implemented the method <code>textView:shouldChangeTextInRanges:replacementStrings:</code> in the next manner:</p> <pre><code>- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRanges:(NSArray *)affectedRanges replacementStrings:(NSArray *)replacementStrings{ NSTextStorage* ts=[textView textStorage]; [ts beginEditing]; NSMutableString* buf=[ts mutableString]; //make some completions NSRange rng; rng.location=0; rng.length=[buf length]; [buf replaceOccurrencesOfString:@"(." withString:@"(0." options:0 range:rng]; rng.location=0; rng.length=[buf length]; [buf replaceOccurrencesOfString:@"-+" withString:@"-" options:0 range:rng]; rng.location=0; rng.length=[buf length]; [buf replaceOccurrencesOfString:@"+-" withString:@"-" options:0 range:rng]; [ts endEditing]; return YES; } </code></pre> <p>Thus I try the next scenario: when the application is up, the user types inside the <code>NSTextView</code> some text. If he types "(." everything is going well. But when he types "-+ " (minus, plus, whitespace) - the method crashes producing this dump:</p> <pre><code>[NSConcreteTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds ( 0 CoreFoundation 0x00007fff95963286 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff8c32bd5e objc_exception_throw + 43 2 CoreFoundation 0x00007fff959630ba +[NSException raise:format:arguments:] + 106 3 CoreFoundation 0x00007fff95963044 +[NSException raise:format:] + 116 4 AppKit 0x00007fff90acb5bd -[NSConcreteTextStorage attributesAtIndex:effectiveRange:] + 121 5 AppKit 0x00007fff90f4cde6 -[NSTextView insertText:replacementRange:] + 840 6 AppKit 0x00007fff910fc891 -[NSTextInputContext handleTSMEvent:] + 2289 7 AppKit 0x00007fff910fe4fe _NSTSMEventHandler + 165 8 HIToolbox 0x00007fff89745308 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1263 9 HIToolbox 0x00007fff89744914 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 446 10 HIToolbox 0x00007fff8975b6c7 SendEventToEventTarget + 76 11 HIToolbox 0x00007fff89930a4d SendTSMEvent + 35 12 HIToolbox 0x00007fff89930dd4 SendUnicodeTextAEToUnicodeDoc + 452 13 HIToolbox 0x00007fff8993095e TSMKeyEvent + 629 14 HIToolbox 0x00007fff8976f81a TSMProcessRawKeyEvent + 2350 15 AppKit 0x00007fff910fbade -[NSTextInputContext handleEvent:] + 639 16 AppKit 0x00007fff90fc7eaf -[NSView interpretKeyEvents:] + 248 17 AppKit 0x00007fff90f40c65 -[NSTextView keyDown:] + 691 18 AppKit 0x00007fff90a20544 -[NSWindow sendEvent:] + 7430 19 AppKit 0x00007fff909b868f -[NSApplication sendEvent:] + 5593 20 AppKit 0x00007fff9094e682 -[NSApplication run] + 555 21 AppKit 0x00007fff90bcd80c NSApplicationMain + 867 22 myapp 0x0000000100001302 main + 34 23 myapp 0x00000001000012d4 start + 52 </code></pre> <p>It seems that shortened string causes some out-of-bounds spill.</p> <p>What am I doing wrong and how can I fix this issue? Please help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload