我正在尝试获取 textView macOS 的插入点
// Get insertion point position
if let selectedRange = textView.selectedRange {
cursorPosition = textView.offset(from: textView.beginningOfDocument, to: selectedRange.start)
}
我收到此错误:
Initializer for conditional binding must have Optional type, not 'NSRange' (aka '_NSRange')
我在 iOS 中使用了类似的代码并且它有效,我做错了什么?
最佳答案
if let
假定赋值的右侧是可选的。 selectedRange
不返回可选值;您应该无需测试即可直接使用它。
关于cocoa - 尝试获取 textView macOS 的插入点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44845909/