如何更改 NStextField 的插入文本(光标)?对于 os x swift 2

How to change insertion text (cursor) for NStextField? for os x swift 2

我找到的最相关的代码是 let fieldEditor = self.window?.fieldEditor(true, forObject: textField) 为! NS文本视图? fieldEditor!.insertionPointColor = NSColor.redColor() 但不起作用,因为字段编辑器是零我在文本字段的子类中尝试使用它。

没关系,通过扩展 NSTextField class 并手动调用函数找到了答案

extension NSTextField {
   public func customizeCaretColor(caretColor: NSColor) {
      let fieldEditor = self.window?.fieldEditor(true, forObject: self) as! NSTextView
      fieldEditor.insertionPointColor = caretColor
   }
}