2016-12-16 4 views
0

다음 코드를 사용하여 NSTextView의 전경색을 얻으려고합니다. 불행히도 관련된 색상 공간과 관련된 런타임 오류가 발생합니다. 어떻게 해결할 수 나는 다음과 같은 런타임 오류 얻을NSTextView의 전경색을 얻을 수 없습니다.

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") 
}  

M :

[General] *** invalid number of components for colorspace in initWithColorSpace:components:count:

답변

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 

편집 : 나는 메모리에 수정을했다 이제는 코드가 흰색이되어야하는 것을 제외하고는 (회색 색 공간 회색 색 공간 ) 노랑색으로 바뀝니다. 누군가가 고칠 수 있기를 바랍니다.