NSTextView,setString:引发异常

时间:2013-09-23 13:14:40

标签: objective-c cocoa nstextview

我一直试图找出这段代码有什么问题:我有一个NSTextView,当用户点击一个表格视图时,我想要更新该字符串。现在,我只是想让它显示字符串“Notes”。

对我做错了什么的任何想法?我查看了beginEditing:和endEditing:没有任何成功。

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


//
//  MyAppDelegate.m

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

这是堆栈:

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
)
编辑:在我的 - (void)applicationDidFinishLaunching:例程期间,我意识到这段代码实际上在程序初始化结束时没有任何异常地运行了两次。之后,任何触发都会出错。这表明了什么?

编辑2:抛开使用insertText的事实:追加而不是替换,我在尝试时得到一个非常类似的异常:

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
)

2 个答案:

答案 0 :(得分:2)

原来我在调用stringValue时正在修改NSTextView的textDidEndEditing:,导致错误的字符串计数和错误计数时出现Range or index out of bounds异常在NSTextView内部方法中使用。

我只是通过使用ivar @property BOOL canModifyTextViewString来绕过这个问题,因为在方法中相应地设置了这种方法,修改字符串是不安全/不可能的。

答案 1 :(得分:1)