2

我一直试图找出这段代码有什么问题:我有一个 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
)
4

2 回答 2

2

事实证明,我在stringValue调用 NSTextView 时正在修改它textDidEndEditing:,这导致字符串计数不匹配,并且在内部方法Range or index out of bounds中使用此错误计数时引发异常。NSTextView

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

于 2013-11-03T09:26:45.047 回答
1

NSTextView没有setString作为类方法

-(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

于 2013-09-23T13:30:31.893 回答