从剪贴板粘贴文本时自定义 NSTextStorage 崩溃

问题描述

我试图子类化 NSTextStorage。现在,我只遵循 document,覆盖原语:

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,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,changeInLength: str.count - range.length)
    endEditing()
}

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

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...