2013-11-20 18 views
0

我的应用程序使用我自己的NSTextStorage的子类,并且一切正常,直到Mavericks。即使使用最精简的版本,只要我用它初始化文本视图,我就会得到一个异常。我阅读了文档,也看了一下How to extend NSTextStorage?。希望你们中的一位知道苹果是否改变了一些事情,或者如果我做错了什么。在小牛上继承NSTextStorage时与范围相关的异常

信息,你可能需要帮我:

例外:

TextViewCheck [12542:303]:异常*** NSRunStorage(0x6000000a4200),_replaceElements():替换范围{0,929}将 延伸超出当前运行存储大小928.在排版布局管理器期间引发 NSLayoutManager:0x100106a80
1容器,文本备份g有928个字符 所选字符范围{0,0}亲和力:上游粒度:字符 标记字符范围{0,0} 当前拥有928个字形。
字形树内容:928个字符,928个字形,1个节点,64个节点字节,960个 存储字节,1024个总字节,每个字符1.10个字节,每个字形1.10个字节 布局树内容:928个字符,928个字形,字符0,放置行 片段,1个节点,64个节点字节,0个存储字节,64个总字节,0.07个字节每个字符 ,每个字形0.07个字节,每个放置线段的0.00个放置字形, 每个放置线段的0.00字节 ,字形范围{0 928}。忽略...

回溯

thread #1: tid = 0x137013, 0x00007fff8ad7e44e AppKit`_NSGlyphTreeGetGlyphsInRange + 1179, queue = 'com.apple.main-thread, stop reason = EXC_BAD_ACCESS (code=1, address=0x1002c0000) 

frame #0: 0x00007fff8ad7e44e AppKit`_NSGlyphTreeGetGlyphsInRange + 1179 
frame #1: 0x00007fff8ad7dfa1 AppKit`-[NSLayoutManager getGlyphsInRange:glyphs:characterIndexes:glyphInscriptions:elasticBits:bidiLevels:] + 92 
frame #2: 0x00007fff8ad4d8b6 AppKit`-[NSATSGlyphStorage setGlyphRange:characterRange:] + 3724 
frame #3: 0x00007fff8ad4c8be AppKit`-[NSATSTypesetter _ctTypesetter] + 306 
frame #4: 0x00007fff8ad4baf9 AppKit`-[NSATSLineFragment layoutForStartingGlyphAtIndex:characterIndex:minPosition:maxPosition:lineFragmentRect:] + 85 
frame #5: 0x00007fff8ad4a64e AppKit`-[NSATSTypesetter _layoutLineFragmentStartingWithGlyphAtIndex:characterIndex:atPoint:renderingContext:] + 2777 
frame #6: 0x00007fff8ad7dc79 AppKit`-[NSATSTypesetter layoutParagraphAtPoint:] + 149 
frame #7: 0x00007fff8b3f5b45 AppKit`-[NSTypesetter _layoutGlyphsInLayoutManager:startingAtGlyphIndex:maxNumberOfLineFragments:maxCharacterIndex:nextGlyphIndex:nextCharacterIndex:] + 4043 
frame #8: 0x00007fff8ad7cc9d AppKit`-[NSTypesetter layoutCharactersInRange:forLayoutManager:maximumNumberOfLineFragments:] + 202 
frame #9: 0x00007fff8ad7cb81 AppKit`-[NSATSTypesetter layoutCharactersInRange:forLayoutManager:maximumNumberOfLineFragments:] + 1107 
frame #10: 0x00007fff8ad7b219 AppKit`-[NSLayoutManager(NSPrivate) _fillLayoutHoleForCharacterRange:desiredNumberOfLines:isSoft:] + 1306 
frame #11: 0x00007fff8ae24ad9 AppKit`_NSFastFillAllLayoutHolesForGlyphRange + 1435 
frame #12: 0x00007fff8ae6ddbd AppKit`-[NSLayoutManager(NSPrivate) _firstPassGlyphRangeForBoundingRect:inTextContainer:okToFillHoles:] + 309 
frame #13: 0x00007fff8ae6cea4 AppKit`-[NSLayoutManager(NSPrivate) _glyphRangeForBoundingRect:inTextContainer:fast:okToFillHoles:] + 875 
frame #14: 0x00007fff8add81b2 AppKit`-[NSTextView setNeedsDisplayInRect:avoidAdditionalLayout:] + 1466 
frame #15: 0x00007fff8acb4222 AppKit`-[NSView setNeedsDisplay:] + 81 
frame #16: 0x00007fff8add68d9 AppKit`-[NSTextView setTextContainer:] + 699 
frame #17: 0x00007fff8add651a AppKit`-[NSTextContainer setTextView:] + 263 
... 

代码:

TTTSimpleTextStorage.m

@implementation TTTSimpleTextStorage 

- (id)initWithAttributedString:(NSAttributedString *)attrStr { 
    if (self = [super init]) { 
     contents = attrStr ? [attrStr mutableCopy] : 
     [[NSMutableAttributedString alloc] init]; 
    } 
    return self; 
} 


- init { 
    return [self initWithAttributedString:nil]; 
} 

- (NSString *)string { 
    return [contents string]; 
} 

- (NSDictionary *)attributesAtIndex:(NSUInteger)location 
       effectiveRange:(NSRange *)range { 
    return [contents attributesAtIndex:location effectiveRange:range]; 
} 

- (void)replaceCharactersInRange:(NSRange)range withString:(NSString 
                 *)str { 
    NSInteger origLen = [self length]; 
    [contents replaceCharactersInRange:range withString:str]; 
    [self edited:NSTextStorageEditedCharacters range:range 
      changeInLength:[self length] - origLen]; 
} 

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range { 
    [contents setAttributes:attrs range:range]; 
    [self edited:NSTextStorageEditedCharacters range:range 
     changeInLength:0]; 
} 

@end 

我如何使用它

这是MainWindowController - awakeFromNib,窗口IBOutlet中有一个的NSView作为子视图命名容器和一个随机字符串字符串1

nts1 = [[TTTSimpleTextStorage alloc] initWithString:string1]; 

NSLayoutManager* lm1 = [[NSLayoutManager alloc] init]; 
[nts1 addLayoutManager:lm1]; 

NSTextContainer* tc1 = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(400, 280)]; 
[lm1 addTextContainer:tc1]; 


tv1 = [[NSTextView alloc] initWithFrame:NSMakeRect(40, 20, 400, 280) textContainer:tc1]; // here it crashes 

[self.container addSubview:tv1]; 
[self.window makeKeyAndOrderFront:nil]; 
[self.window makeFirstResponder:tv1]; 
+1

它看起来像你的一个范围内的错误的错误。 –

+0

@BradAllred:对,但我不知道我在哪里创建该错误或我如何解决它。显然我使用的字符串有928个字符,直到那个崩溃只有方法 - (id)init ..., - (NSString *)字符串和 - (NSDictionary *)attributesAtIndex ...被调用。并在属性的索引范围是(928,0) –

+1

注释掉所有覆盖除第一个和运行它。如果它没有崩溃,请添加下一个方法并重复,直到找到罪魁祸首。我的钱是'replaceCharactersInRange' –

回答

0

在replaceCharactersInRange,要设置origLen到[自身长度],并且一行之后,您将通过[自身长度]减去origLen来计算changeInLength,这是有效的:

NSInteger origLen = self.length; 
// ... 
changeInLength: origLen - self.length 

我觉得你宁愿:

changeInLength: self.length - str.length]; 
+0

嘿,我试了一下,但它没有帮助。与此同时,我写了Apple支持,他们认为这是该框架某处的错误。我将在他们的错误跟踪器中报告一个错误。我的解决方法是不要替换'TextStorage',而是替换它的'String'。 –