如何在Swift中获取C指针的原始值?

时间:2015-12-11 11:08:08

标签: ios swift swift2 core-text

我正在开发一个使用CoreText的项目;我需要初始化CTRunDelegateCallbacks

var  imageCallback =  CTRunDelegateCallbacks(version: kCTRunDelegateCurrentVersion, dealloc: { (refCon) -> Void in
    print("RunDelegate dealloc")
    }, getAscent: { ( refCon) -> CGFloat in
        return 0
    }, getDescent: { (refCon) -> CGFloat in
        return 0
    }) { (refCon) -> CGFloat in
        return 0
}

参数refConUnsafeMutablePointer<Void>类型,在C中也称为void *类型。我想获取指针的原始值。怎么做?

1 个答案:

答案 0 :(得分:1)

我不建议将指针转换为非指针类型。我会告诉你如何在这个答案的最后做到这一点,但首先我会告诉你应该如何处理refCon

创建一个结构来保存您需要传递给回调的任何信息。例如:

struct MyRunExtent {
    let ascent: CGFloat
    let descent: CGFloat
    let width: CGFloat
}

然后使用其UnsafeMutablePointer类方法创建alloc,并初始化分配的存储:

let extentBuffer = UnsafeMutablePointer<MyRunExtent>.alloc(1)
extentBuffer.initialize(MyRunExtent(ascent: 12, descent: 4, width: 10))

在你的回调中,将指针参数转换为UnsafePointer<MyRunExtent>并从memory中提取所需内容:

var callbacks = CTRunDelegateCallbacks(version: kCTRunDelegateVersion1, dealloc: { pointer in
    pointer.dealloc(1)
    }, getAscent: { pointer in
        return UnsafePointer<MyRunExtent>(pointer).memory.ascent
    }, getDescent: { pointer in
        return UnsafePointer<MyRunExtent>(pointer).memory.descent
    }, getWidth: { pointer in
        return UnsafePointer<MyRunExtent>(pointer).memory.width
})

现在,您可以使用callbacksextentBuffer

创建代理
let delegate = CTRunDelegateCreate(&callbacks, extentBuffer)!

这是一个测试:

let richText = NSMutableAttributedString(string: "hello \u{FFFC} world")
richText.addAttribute(kCTRunDelegateAttributeName as String, value: delegate, range: NSMakeRange(6, 1))
let line = CTLineCreateWithAttributedString(richText)
let runs = (CTLineGetGlyphRuns(line) as [AnyObject]).map { $0 as! CTRun }
runs.forEach {
    var ascent = CGFloat(0), descent = CGFloat(0), leading = CGFloat(0)
    let width = CTRunGetTypographicBounds($0, CFRangeMake(0, 0), &ascent, &descent, &leading)
    print("width:\(width) ascent:\(ascent) descent:\(descent) leading:\(leading)")
}

输出(在操场上):

2015-12-21 12:26:00.505 iOS Playground[17525:8055669] -[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x7f94bcb01dc0
width:28.6875 ascent:9.240234375 descent:2.759765625 leading:0.0
width:10.0 ascent:12.0 descent:4.0 leading:0.0
width:32.009765625 ascent:9.240234375 descent:2.759765625 leading:0.0

第一行输出是因为操场执行过程无法对委托进行编码以发送回Xcode进行显示,结果证明是无害的。无论如何,您可以看到中间运行的界限是使用我的回调和我的extentBuffer的内容计算的。

现在,你等待的那一刻......

您可以通过这种方式获取指针的“原始值”,如果指针和Int在运行的系统上的大小相同:

let rawValue = unsafeBitCast(refCon, Int.self)

如果它们的大小不同,您将在运行时遇到致命错误。

您可以通过这种方式将其投放到CGFloat如果指针和CGFloat的尺寸相同:

let float = unsafeBitCast(refCon, CGFloat.self)