سؤال

I've been trying to find out what's wrong with this piece of code: I have an NSTextView which string I want to update when the user clicks in one one my table views. For now on, I'm just trying to get it to display the string "Notes".

Any idea on what I am doing wrong ? I looked into beginEditing: and endEditing: without any success.

//
//  MyAppDelegate.h
@property IBOutlet NSTextView* stickyNoteContainer;


//
//  MyAppDelegate.m

-(void)updateOverview{
    [stickyNoteContainer setString:@"Notes"];
}

Here's the stack :

MyApp[1176:303] *** -[NSBigMutableString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range or index out of bounds
2013-09-23 15:04:12.084 MyApp[1176:303] (
    0   CoreFoundation                      0x00007fff917eaf56 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff95bb7d5e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff917ead8a +[NSException raise:format:arguments:] + 106
    3   CoreFoundation                      0x00007fff917ead14 +[NSException raise:format:] + 116
    4   Foundation                          0x00007fff955afbc5 -[NSString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:] + 160
    5   Foundation                          0x00007fff955afb1e -[NSString getParagraphStart:end:contentsEnd:forRange:] + 40
    6   AppKit                              0x00007fff8e5c1cd7 _NSFastFillAllLayoutHolesForGlyphRange + 1040
    7   AppKit                              0x00007fff8eb31bb4 -[NSTextView(NSPrivate) _ensureLayoutCompleteForVisibleRectWithExtensionFactor:minimumExtensionDistance:repetitions:] + 563
    8   AppKit                              0x00007fff8eb2d13c -[NSTextView(NSPrivate) _ensureLayoutCompleteForVisibleRectWithExtension:] + 45
    9   AppKit                              0x00007fff8e6b9650 -[NSTextView setNeedsDisplayInRect:avoidAdditionalLayout:] + 2078
    10  AppKit                              0x00007fff8e6bf1c1 -[NSLayoutManager(NSPrivate) _resizeTextViewForTextContainer:] + 5690
    11  AppKit                              0x00007fff8e5b0110 -[NSLayoutManager(NSPrivate) _recalculateUsageForTextContainerAtIndex:] + 2678
    12  AppKit                              0x00007fff8e96b948 _enableTextViewResizing + 209
    13  AppKit                              0x00007fff8e5a8454 -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 582
    14  AppKit                              0x00007fff8e5a81f6 -[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 155
    15  AppKit                              0x00007fff8e681133 -[NSTextStorage processEditing] + 205
    16  AppKit                              0x00007fff8e681cab -[NSTextStorage endEditing] + 81
    17  MyApp                           0x0000000100007938 -[MyAppDelegate updateOverview] + 1624
    18  MyApp                           0x0000000100008751 -[MyAppDelegate tableViewSelectionDidChange:] + 177
    19  Foundation                          0x00007fff95550d0e __-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_1 + 47
    20  CoreFoundation                      0x00007fff917937ba _CFXNotificationPost + 2634
    21  Foundation                          0x00007fff9553cfc3 -[NSNotificationCenter postNotificationName:object:userInfo:] + 65
    22  AppKit                              0x00007fff8e674049 -[NSTableView _sendSelectionChangedNotificationForRows:columns:] + 203
    23  AppKit                              0x00007fff8e64ab0a -[NSTableView _enableSelectionPostingAndPost] + 425
    24  AppKit                              0x00007fff8e670e58 -[NSTableView mouseDown:] + 5030
    25  AppKit                              0x00007fff8e5d6c98 -[NSWindow sendEvent:] + 6306
    26  AppKit                              0x00007fff8e5703a5 -[NSApplication sendEvent:] + 5593
    27  AppKit                              0x00007fff8e506a0e -[NSApplication run] + 555
    28  AppKit                              0x00007fff8e782eac NSApplicationMain + 867
    29  MyApp                           0x0000000100002002 main + 34
    30  MyApp                           0x0000000100001fd4 start + 52
)

EDIT : I realized the piece of code actually runs twice without any exception at the end of the program initialization, during my - (void)applicationDidFinishLaunching: routine. After that, any triggering will be faulty. What does this suggest ?

EDIT 2 : Put aside the fact using insertText: appends instead of replacing, I get a very similar exception when trying :

MyApp[1919:303] *** -[NSBigMutableString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range or index out of bounds
2013-09-23 15:34:03.956 MyApp[1919:303] (
    0   CoreFoundation                      0x00007fff917eaf56 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff95bb7d5e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff917ead8a +[NSException raise:format:arguments:] + 106
    3   CoreFoundation                      0x00007fff917ead14 +[NSException raise:format:] + 116
    4   Foundation                          0x00007fff955afbc5 -[NSString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:] + 160
    5   Foundation                          0x00007fff955afb1e -[NSString getParagraphStart:end:contentsEnd:forRange:] + 40
    6   AppKit                              0x00007fff8e5c1cd7 _NSFastFillAllLayoutHolesForGlyphRange + 1040
    7   AppKit                              0x00007fff8eb31bb4 -[NSTextView(NSPrivate) _ensureLayoutCompleteForVisibleRectWithExtensionFactor:minimumExtensionDistance:repetitions:] + 563
    8   AppKit                              0x00007fff8eb2d13c -[NSTextView(NSPrivate) _ensureLayoutCompleteForVisibleRectWithExtension:] + 45
    9   AppKit                              0x00007fff8e6b9650 -[NSTextView setNeedsDisplayInRect:avoidAdditionalLayout:] + 2078
    10  AppKit                              0x00007fff8e6bf1c1 -[NSLayoutManager(NSPrivate) _resizeTextViewForTextContainer:] + 5690
    11  AppKit                              0x00007fff8e5b0110 -[NSLayoutManager(NSPrivate) _recalculateUsageForTextContainerAtIndex:] + 2678
    12  AppKit                              0x00007fff8e96b948 _enableTextViewResizing + 209
    13  AppKit                              0x00007fff8e5a8454 -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 582
    14  AppKit                              0x00007fff8e5a81f6 -[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 155
    15  AppKit                              0x00007fff8e681133 -[NSTextStorage processEditing] + 205
    16  AppKit                              0x00007fff8e681cab -[NSTextStorage endEditing] + 81
    17  AppKit                              0x00007fff8eb0620f -[NSTextView insertText:replacementRange:] + 2013
    18  AppKit                              0x00007fff8eb05a25 -[NSTextView insertText:] + 320
    19  MyApp                      0x00000001000078c1 -[MyAppDelegate updateOverview] + 1569
    20  MyApp                      0x0000000100008751 -[MyAppDelegate tableViewSelectionDidChange:] + 177
    21  Foundation                          0x00007fff95550d0e __-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_1 + 47
    22  CoreFoundation                      0x00007fff917937ba _CFXNotificationPost + 2634
    23  Foundation                          0x00007fff9553cfc3 -[NSNotificationCenter postNotificationName:object:userInfo:] + 65
    24  AppKit                              0x00007fff8e674049 -[NSTableView _sendSelectionChangedNotificationForRows:columns:] + 203
    25  AppKit                              0x00007fff8e64ab0a -[NSTableView _enableSelectionPostingAndPost] + 425
    26  AppKit                              0x00007fff8e670e58 -[NSTableView mouseDown:] + 5030
    27  AppKit                              0x00007fff8e5d6c98 -[NSWindow sendEvent:] + 6306
    28  AppKit                              0x00007fff8e5703a5 -[NSApplication sendEvent:] + 5593
    29  AppKit                              0x00007fff8e506a0e -[NSApplication run] + 555
    30  AppKit                              0x00007fff8e782eac NSApplicationMain + 867
    31  MyApp                           0x0000000100001fc2 main + 34
    32  MyApp                           0x0000000100001f94 start + 52
)
هل كانت مفيدة؟

المحلول

It turned out I was modifying the stringValue of a NSTextView while it was calling textDidEndEditing:, which resulted in a mismatched string count and a Range or index out of bounds exception thrown when this wrong count was being used inside NSTextView inner methods.

I just bypassed the problem by using an ivar @property BOOL canModifyTextViewString which would be set accordingly inside methods where modifying the string is unsafe/not possible.

نصائح أخرى

NSTextView doesn't have setString as a class method

-(void)updateOverview{
[stickyNoteContainer insertText:@"Notes"];
}

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextView_Class/Reference/Reference.html#//apple_ref/occ/instm/NSTextView/insertText:

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top