我正试图用Swift在MacOSX应用程序中捕获一个窗口列表。cgWindowListCreateImageFromArray函数需要cfArray。我试过几次了,这是我最近的一次。或者有更好的方法来转换数组吗?

import Cocoa

// Example swift array of CGWindowID's
var windowIDs = [CGWindowID]();
windowIDs.append(1);
windowIDs.append(2);

// Convert to CFArray using CFArrayCreate
let allocator = kCFAllocatorDefault
let numValues = windowIDs.count as CFIndex
let callbacks: UnsafePointer<CFArrayCallBacks> = nil
var values: UnsafeMutablePointer<UnsafePointer<Void>> = nil

/* how do I convert windowIDs to UnsafeMutablePointer<UnsafePointer<Void>> for the values? */

let windowIDsCFArray = CFArrayCreate(allocator, values,  numValues, callbacks);

let capture = CGWindowListCreateImageFromArray(CGRectInfinite, windowIDsCFArray, CGWindowImageOption(kCGWindowListOptionOnScreenOnly));

最佳答案

只要将UnsafeMutablePointers设置为CGWindowID,就可以使用数组初始化CFTypeRef

var windows: [CFTypeRef] = [1, 2]
var windowsPointer = UnsafeMutablePointer<UnsafePointer<Void>>(windows)
var cfArray = CFArrayCreate(nil, windowsPointer, windows.count, nil)

关于objective-c - 如何将Swift数组转换为CFArray?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28947049/

10-10 00:23