Objective c NSTextStorage AppendAttributeString:未捕获异常
我想做一个irc客户端。我保留了一个NSTextView对象,还有一个容器类保存了一个NSTextStorage对象。每次用户单击通道按钮时,我都使用NSTextView的layoutManager替换NSTextStorage对象。存储的文本是NSAttributedString的形式,因为我需要不同颜色的文本部分 在NSTextStorage中存储NSAttributedString的方法是从容器对象获取指针,并在其上使用appendAttributedString 我在NSLog中得到了一个未捕获的异常,没有中断。你能帮我弄清楚怎么了吗Objective c NSTextStorage AppendAttributeString:未捕获异常,objective-c,cocoa,nstextview,nstextstorage,Objective C,Cocoa,Nstextview,Nstextstorage,我想做一个irc客户端。我保留了一个NSTextView对象,还有一个容器类保存了一个NSTextStorage对象。每次用户单击通道按钮时,我都使用NSTextView的layoutManager替换NSTextStorage对象。存储的文本是NSAttributedString的形式,因为我需要不同颜色的文本部分 在NSTextStorage中存储NSAttributedString的方法是从容器对象获取指针,并在其上使用appendAttributedString 我在NSLog中得到了一
2015-09-14 08:25:49.776 ircX[3248:128903] *** -[NSConcreteTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
2015-09-14 08:25:49.780 ircX[3248:128903] (
0 CoreFoundation 0x00007fff9974503c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff99c0176e objc_exception_throw + 43
2 CoreFoundation 0x00007fff99744eed +[NSException raise:format:] + 205
3 UIFoundation 0x00007fff939c7d77 -[NSConcreteTextStorage attributesAtIndex:effectiveRange:] + 127
4 AppKit 0x00007fff94e05ffc -[NSTextView(NSSharing) setSelectedRanges:affinity:stillSelecting:] + 2525
5 UIFoundation 0x00007fff939a53af -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 423
6 UIFoundation 0x00007fff939c7291 -[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 152
7 UIFoundation 0x00007fff939c6d93 -[NSTextStorage processEditing] + 198
8 UIFoundation 0x00007fff939c6c98 -[NSTextStorage edited:range:changeInLength:] + 312
9 Foundation 0x00007fff925935e3 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 330
10 UIFoundation 0x00007fff939c87c1 __71-[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:]_block_invoke + 52
11 UIFoundation 0x00007fff939c8703 -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 136
12 ircX 0x000000010000b68b -[IRCWindow addData:] + 3371
13 ircX 0x000000010000195f -[ViewController updateTextView] + 527
14 Foundation 0x00007fff9259f953 __NSFireTimer + 95
15 CoreFoundation 0x00007fff996982e4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
16 CoreFoundation 0x00007fff99697f73 __CFRunLoopDoTimer + 1059
17 CoreFoundation 0x00007fff9970b53d __CFRunLoopDoTimers + 301
18 CoreFoundation 0x00007fff99653608 __CFRunLoopRun + 2024
19 CoreFoundation 0x00007fff99652bd8 CFRunLoopRunSpecific + 296
20 HIToolbox 0x00007fff962a956f RunCurrentEventLoopInMode + 235
21 HIToolbox 0x00007fff962a92ea ReceiveNextEventCommon + 431
22 HIToolbox 0x00007fff962a912b _BlockUntilNextEventMatchingListInModeWithFilter + 71
23 AppKit 0x00007fff94d6c8ab _DPSNextEvent + 978
24 AppKit 0x00007fff94d6be58 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 346
25 AppKit 0x00007fff94d61af3 -[NSApplication run] + 594
26 AppKit 0x00007fff94cde244 NSApplicationMain + 1832
27 ircX 0x00000001000020b2 main + 34
28 libdyld.dylib 0x00007fff8ec045c9 start + 1
)
错误消息说,在调用attributesAtIndex:effectiveRange:时,索引或范围超出了范围,因此请检查值我知道这些值是否有错误。调试器不会说谎。问题是,我从来没有把索引或范围放在首位。我只是使用appendAttributedString,它工作得很好,但在尝试提交到其他NSTextStorage对象并切换它们时出错。你能想出一个潜在的原因吗?可能是。你提供的信息太少,无法了解更多。很难说是什么原因导致加薪,但我还是建议采用不同的设计。不交换不同的NSTextStorage对象;那是一件奇怪的事。在容器对象中保留NSAttributedString,并通过执行-BeginEdit、-setAttributedString和-EndEdit将新内容交换到现有文本存储中。Re:不过,你在上面的评论中明确指出,你确实对范围做了一些事情;您的-[IRCSWindow addData:]方法根据回溯调用-[NSContextStorage ReplaceCharactersRange:withAttributedString:]。我将查看其中的范围。appendAttributedString:calls ReplaceCharactersRange:withAttributedString:不带新堆栈帧尾部调用。