gpt4 book ai didi

ios - NSTextStorage 子类在删除字符时崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:15 24 4
gpt4 key购买 nike

我有一个 NSTextStorage 的子类,它给我带来了一些问题。每次我执行以下操作时都会崩溃:

  1. 在第一行输入一些文字
  2. 按返回移动到下一行
  3. 至少输入两个字符
  4. 点击退格键

我遇到的错误是Termating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The index -4097 is invalid'

真正的问题是我无法将错误追溯到我自己的任何代码。我可以通过调试得到的最远的是 processEditing 覆盖内的 super.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()
}

最佳答案

嗯,虽然我仍然不知道为什么会发生崩溃,但我设法找到了一些解决方法。它似乎与布局约束或我的富文本编辑器的 TextView 的大小有关,因为在我从 Storyboard 中删除 TextView 并以编程方式创建它(连同 TextContainer 和 NSLayoutManager)后,崩溃不再发生。

关于ios - NSTextStorage 子类在删除字符时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39999806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com