我有一个NSTextStorage子类,这给我带来了一些问题。每次做以下动作我都会崩溃:
我得到的错误是Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The index -4097 is invalid'
真正的问题是,我无法将错误追溯到我自己的代码中。通过调试,我能得到的最大好处是调用了super.processEditing在processEditing的覆盖范围内。堆叠痕迹也没有给我任何东西可以处理。
编辑:做了更多的测试,发现这只发生在iOS 9和更高版本上。8或以下的任何东西都不会崩溃。
override func attributesAtIndex(location: Int, effectiveRange range: NSRangePointer) -> [String : AnyObject] {
return backingStore.attributesAtIndex(location, effectiveRange: range)
}
override func replaceCharactersInRange(range: NSRange, withString str: String) {
beginEditing()
backingStore.replaceCharactersInRange(range, withString: str)
edited([.EditedCharacters, .EditedAttributes], range: range, changeInLength: (str as NSString).length - range.length)
endEditing()
}
override func setAttributes(attrs: [String : AnyObject]?, range: NSRange) {
beginEditing()
backingStore.setAttributes(attrs, range: range)
edited(.EditedAttributes, range: range, changeInLength: 0)
endEditing()
}
override func setAttributedString(attrString: NSAttributedString) {
programmaticChange = true
super.setAttributedString(attrString)
programmaticChange = false
}
override func processEditing() {
if (!programmaticChange &&
(editedMask.rawValue & NSTextStorageEditActions.EditedCharacters.rawValue) == NSTextStorageEditActions.EditedCharacters.rawValue &&
changeInLength > 0) {
doSetAttributesForRange(editedRange)
}
print(backingStore)
super.processEditing()
}发布于 2016-10-18 06:41:59
嗯,我设法找到了一些解决办法,虽然我仍然不知道为什么会发生崩溃。它似乎与布局约束或我的富文本编辑器的TextView大小有关,因为在我从故事板中删除TextView并以编程方式创建它(以及TextContainer和NSLayoutManager)之后,崩溃不再发生。
发布于 2019-06-15 06:31:26
我有这个问题,下面是为我解决的。
在设置UITextView布局管理器的自定义文本存储子类后,禁用XIB文件中的UITextViews滚动,然后以编程方式重新启用它。
https://stackoverflow.com/questions/39999806
复制相似问题