如何将文本附加到 NSTextView 实例的开头?

How do I append text to the begging of a NSTextView instance?

在我的 macOS 应用程序中,NSTextView 显示了一些日志。生成日志时,我使用 NSTextView.textStorage.append(NSAttributedString) 将其添加到带有换行符的视图末尾。

(1) 我不确定如何将日志添加到视图的开头。

(2) 我觉得每个日志应该是一个单独的元素而不是一个带有换行符的字符串,我应该使用其他方式来构建这个日志视图吗?

class LogView: NSTextView {

    func log(text: String) {
        DispatchQueue.main.async {
            self.textStorage?.append(NSAttributedString(string: "\(text)\n", attributes: [NSAttributedStringKey.foregroundColor: NSColor.white]))
        }
    }
}

textStorage 继承自 NSAttributedString 如果有 append 也有 insert(at:)

class LogView: NSTextView {

    func log(text: String) {
        DispatchQueue.main.async {
            self.textStorage?.insert(NSAttributedString(string: "\(text)\n", attributes: [NSAttributedStringKey.foregroundColor: NSColor.white]), at: 0)
        }
    }
}