I was trying to subclass NSTextStorage. For now, I just follow the document, override for primitives:
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)
}
}
At first, everything works fine. However, when I try to paste a long text (about 9000 characters) to a UITextView using my customized TextStorage, my app crashed:
(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
I don't think 10k is too large for a TextView, and it works fine if I use the default text storage. But I can not find where the problem is.
I've tried use Objective-C instead of Swift to write my TextStorage, and the problem remains:
#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
Tried adding beginEditing() and endEditing() like this, but it doesn't make any different:
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()
}
I found that the crash is happened after the replaceCharacters(in:with:) and setAttributes(_:range:), under the processEditing() call. But I didn't override processEditing().