我正在尝试使用后面的代码来获取NSTextView的前景色。不幸的是,我得到一个必须与涉及的颜色空间相关的运行时错误。我该如何修理m
if let textStorage = textView.textStorage {
let rowObj = textStorage.paragraphs[row]
let range = NSMakeRange(0, rowObj.string.characters.count)
colorOrRowBeforeSelection = rowObj.foregroundColor!
if(rowObj.foregroundColor != nil) {
let r = rowObj.foregroundColor!.redComponent
let g = rowObj.foregroundColor!.greenComponent
let b = rowObj.foregroundColor!.blueComponent
} else {
Log.e("cannot get foreground color components")
}
} else {
Log.e("textStorage = nil")
}
我收到以下运行时错误:
[General] *** initWithColorSpace中色彩空间的无效组件数:components:count:
答案 0 :(得分:0)
我使用了以下内容:
let components = UnsafeMutablePointer<CGFloat>.allocate(capacity: 4)
rowObj.foregroundColor!.getComponents(components)
let c = NSColor(colorSpace: NSColorSpace.sRGB, components: components, count: 4)
components.deinitialize()
components.deallocate(capacity: 4)
let r = c.redComponent
let g = c.greenComponent
let b = c.blueComponent
编辑:我修改了内存处理,现在代码工作,但事实上它应该是白色(在灰色空间)变成黄色...希望有人可以解决这个问题。