string.length keypath 绑定在 Sonoma 上的 nib 解码期间崩溃

问题描述 投票:0回答:1

从 macOS 14.0 收到一些崩溃报告。深入挖掘后,我发现问题与键路径绑定(

NSDisplayPatternValueBinding
)与
NSObjectController
NSTextView
有关。我将
NSTextView
绑定到 Storyboard 中的
NSObjectController
并在代码中设置
NSObjectController
的内容。内容是一个简单的
String
包装。

当我将 keypath 与“dot”(text.length)一起使用时,就会出现问题。如果我在从笔尖取消存档期间为已知的系统定义的文本替换(“在我的路上!”)设置字符串值 - 应用程序将崩溃并显示以下错误消息。但是,如果该值不在文本替换字典中,则不会崩溃。同样,对于简单的非点键路径,它也不会崩溃。如果我将模型对象更改为

NSString

,它不会崩溃

这是 macOS Sonoma 错误还是我以不应该的方式使用了绑定?

错误信息(异常断点):

(lldb) po $x0
Cannot remove an observer <NSKeyValueObservance 0x6000031d0030> for the key path "length" from <Swift.__StringStorage 0x600002a7fa80> because it is not registered as an observer.
(null)

(lldb) po $x0
Cannot update for observer <NSObjectController 0x600000e78500> for the key path "text.length" from <KVOTextViewIssue.DymmyObject 0x600003f713a0>, most likely because the value for the key "text" has changed without an appropriate KVO notification being sent. Check the KVO-compliance of the KVOTextViewIssue.DymmyObject class.
(null)

Failed to set (contentViewController) user defined inspected property on (NSWindow): Cannot update for observer <NSObjectController 0x6000027f6620> for the key path "text.length" from <KVOTextViewIssue.DymmyObject 0x6000016be100>, most likely because the value for the key "text" has changed without an appropriate KVO notification being sent. Check the KVO-compliance of the KVOTextViewIssue.DymmyObject class.

最小可重现示例:

final class DymmyObject : NSObject {
    @objc dynamic var text : String = "On my way!" //CRASHES
    //@objc dynamic var text : NSString = "On my way!" //DOES NOT CRASH
    //@objc dynamic var text : String = "Hello" //DOES NOT CRASH
}

class ViewController: NSViewController {

    @IBOutlet var textView: NSTextView!
    @IBOutlet var objectController: NSObjectController! {
        didSet {
            representedObject = dummy
        }
    }
    var dummy : DymmyObject = DymmyObject()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.textView.insertText("123", replacementRange: NSMakeRange(0, 0))
    }
}

深入挖掘 - 注意到崩溃时的后台线程是拼写检查器

macos cocoa key-value-observing cocoa-bindings nstextview
1个回答
0
投票

将最低 macOS 部署目标从 10.13 提高到 10.14 解决了 Sonoma 上的崩溃/问题。

© www.soinside.com 2019 - 2024. All rights reserved.