gpt4 book ai didi

swift - 如何在 Swift 中使用 UnsafeMutablePointer

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:42 25 4
gpt4 key购买 nike

如何使用 UnsafeMutablePointer<OpaquePointer>在 Swift 中使用一些 Core Foundation 框架?为什么有一个 UnsafeMutablePointer<OpaquePointer>

给定,一般:一些UnsafeMutablePointer<SomeType>其中 typealias SomeType = OpaquePointer

具体示例 API

// SOURCE: import ApplicationServices.PrintCore
typealias PMPrinter = OpaquePointer
func PMSessionGetCurrentPrinter(_ printSession: PMPrintSession, _ currentPrinter: UnsafeMutablePointer<PMPrinter>)
func PMPrinterGetPaperList(PMPrinter, UnsafeMutablePointer<Unmanaged<CFArray>?>)

具体示例用例:获取打印机支持的纸张列表

let printInfo = NSPrintInfo.shared()
let printSession = PMPrintSession(printInfo.pmPrintSession())
var currentPrinterOptional: PMPrinter? = nil
PMSessionGetCurrentPrinter(printSession, &currentPrinterOptional!)
guard let currentPrinter = currentPrinterOptional else { return }

// Get the array of pre-defined PMPapers this printer supports.
// PMPrinterGetPaperList(PMPrinter, UnsafeMutablePointer<Unmanaged<CFArray>?>)
var paperListUnmanaged: Unmanaged<CFArray>?
PMPrinterGetPaperList(currentPrinter, &paperListUnmanaged)
guard let paperList = paperListUnmanaged?.takeUnretainedValue() as [AnyObject]? else { return }

观察到的错误

编译的内容不运行。看起来(也许)合理的语法无法编译。

上面的示例得到以下(预期的)运行时“ fatal error :在展开可选值时意外发现 nil”

一些选择其他尝试:

// Compile Error: Address of variable 'currentPrinter' taken before is is initialized
var currentPrinter: PMPrinter
PMSessionGetCurrentPrinter(printSession, &currentPrinter)

// Compile Error: Nil cannot initialze specified type 'PMPrinter' (aka 'OpaquePointer')
var currentPrinter: PMPrinter = nil
PMSessionGetCurrentPrinter(printSession, &currentPrinter)

// Compile Error: Variable 'currentPrinterPtr' used before being initialized
var currentPrinterPtr: UnsafeMutablePointer<PMPrinter>
PMSessionGetCurrentPrinter(printSession, currentPrinterPtr)

// Compile OK: actually compiles
// Runtime Error: unexpectedly found nil while unwrapping an Optional value
var currentPrinterOptional: PMPrinter? = nil
PMSessionGetCurrentPrinter(printSession, &currentPrinterOptional!)

资源

Apple: Core Printing ⇗
Apple: Using Swift with Cocoa and Objective-C ⇗

虽然文档中包含有用的信息,但 UnsafeMutablePointer<PMPrinter> 的可行实现typealias 为 UnsafeMutablePointer<OpaquePointer>一直难以捉摸。

最佳答案

PMPrinterPMPaper 在 PrintCore 框架中定义作为指向“不完整类型”的指针

typedef struct OpaquePMPrinter*         PMPrinter;
typedef struct OpaquePMPaper* PMPaper;

它们作为 OpaquePointer 导入到 Swift 中,有点使用起来很麻烦。

PMSessionGetCurrentPrinter() 的第二个参数是指向非可选 PMPrinter 变量,在 Swift 中它必须是在作为 inout 参数传递之前进行初始化。一种可能的方式初始化空指针是使用 unsafeBitCast

从数组中获取 PMPaper 对象的最简单方法似乎是是使用 CFArrayGetValueAtIndex() 而不是将其桥接到快速数组。返回一个可以转换的 UnsafeRawPointerOpaquePointer

这在我的测试中有效:

let printInfo = NSPrintInfo.shared()
let printSession = PMPrintSession(printInfo.pmPrintSession())

var currentPrinter = unsafeBitCast(0, to: PMPrinter.self)
PMSessionGetCurrentPrinter(printSession, &currentPrinter);

var paperListUnmanaged: Unmanaged<CFArray>?
PMPrinterGetPaperList(currentPrinter, &paperListUnmanaged)
guard let paperList = paperListUnmanaged?.takeUnretainedValue() else {
fatalError()
}
for idx in 0..<CFArrayGetCount(paperList) {
let paper = PMPaper(CFArrayGetValueAtIndex(paperList, idx))!
var width = 0.0, height = 0.0
PMPaperGetWidth(paper, &width)
PMPaperGetHeight(paper, &height)
print(width, height)
}

关于swift - 如何在 Swift 中使用 UnsafeMutablePointer<OpaquePointer>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44893912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com