1

我试图继承 NSTextStorage。现在,我只关注文档,覆盖原语:

class TextStorage: NSTextStorage {
    private lazy var imp: NSTextStorage = NSTextStorage()

    override var string: String {
        imp.string
    }

    override func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [NSAttributedString.Key : Any] {
        return imp.attributes(at: location, effectiveRange: range)
    }

    override func replaceCharacters(in range: NSRange, with str: String) {
        imp.replaceCharacters(in: range, with: str)
        edited(.editedCharacters, range: range, changeInLength: str.count - range.length)
    }

    override func setAttributes(_ attrs: [NSAttributedString.Key : Any]?, range: NSRange) {
        imp.setAttributes(attrs, range: range)
        edited(.editedAttributes, range: range, changeInLength: 0)
    }
}

起初,一切正常。但是,当我尝试使用自定义的 TextStorage 将长文本(大约 9000 个字符)粘贴到 UITextView 时,我的应用程序崩溃了:

(lldb) thread backtrace
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x16ee2c000)
  * frame #0: 0x00000001f2a1364c libsystem_platform.dylib`_platform_memmove + 76
    frame #1: 0x00000001a6efc910 CoreFoundation`CFStorageGetValues + 864
    frame #2: 0x00000001a81daa5c Foundation`-[NSBigMutableString getCharacters:range:] + 168
    frame #3: 0x00000001b024d724 UIFoundation`__NSGetClusterHeadWithLimit + 1024
    frame #4: 0x00000001b024a29c UIFoundation`_NSFastFillAllGlyphHolesForCharacterRange + 756

我不认为 10k 对于 TextView 来说太大了,如果我使用默认的文本存储,它就可以正常工作。但我找不到问题出在哪里。

我尝试使用 Objective-C 而不是 Swift 来编写我的 TextStorage,但问题仍然存在:

#import "TextStorage.h"

@interface TextStorage ()
@property (nonatomic, strong) NSTextStorage *imp;
@end

@implementation TextStorage

- (NSString *)string
{
    return self.imp.string;
}

- (NSDictionary<NSAttributedStringKey,id> *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range
{
    return [self.imp attributesAtIndex:location effectiveRange:range];
}

- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
{
    [self.imp replaceCharactersInRange:range withString:str];
    [self edited:NSTextStorageEditedCharacters range:range changeInLength:str.length - range.length];
}

- (void)setAttributes:(NSDictionary<NSAttributedStringKey,id> *)attrs range:(NSRange)range
{
    [self.imp setAttributes:attrs range:range];
    [self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
}

- (NSTextStorage *)imp
{
    if (!_imp) {
        _imp = [[NSTextStorage alloc] init];
    }
    return _imp;
}

@end

尝试添加beginEditing()endEditing()喜欢这样,但它没有任何不同:

override func replaceCharacters(in range: NSRange, with str: String) {
    beginEditing()
    imp.replaceCharacters(in: range, with: str)
    edited(.editedCharacters, range: range, changeInLength: str.count - range.length)
    endEditing()
}

override func setAttributes(_ attrs: [NSAttributedString.Key : Any]?, range: NSRange) {
    beginEditing()
    imp.setAttributes(attrs, range: range)
    edited(.editedAttributes, range: range, changeInLength: 0)
    endEditing()
}

我发现崩溃是在 and 之后发生的replaceCharacters(in:with:)setAttributes(_:range:)processEditing()调用下。但我没有覆盖processEditing().

4

0 回答 0