Swift:在CFArray中提取/向下转换基于CFType的CoreTexttypes

我想将CoreAnimationText示例的元素移植到Swift中。 但是我不知道如何从数组中提取或向下转换CTRun的元素,以便将它们传递给期望并在Swift ified CTRuntypes上执行的函数。 我要么得到运行时错误或从下面的游乐场片段链接错误

import CoreText import QuartzCore let text = NSAttributedString(string: "hello") var line: CTLine = CTLineCreateWithAttributedString(text) var ctRuns:CFArray = CTLineGetGlyphRuns(line) let nsRuns:Array<AnyObject> = ctRuns as NSArray nsRuns.count // == 1 // Playground execution failed: error: error: Couldn't lookup symbols:_OBJC_CLASS_$_CTRun let nsRun = nsRuns[0] as CTRun nsRun.self println(nsRun.self) let anyRuns = nsRuns as AnyObject[] // can't unwrap Optional null let anyRun = anyRuns[0] as CTRun anyRun.self println(anyRun.self) let cft:AnyObject = anyRuns[0] // CTRun is not contstructable with AnyObject let runGlyphCount = CTRunGetGlyphCount(CTRun(cft)); // Can't unwrap Optional.None let concreteRuns = anyRuns as CTRun[] let concreteRun = concreteRuns[0] as CTRun concreteRun.self println(concreteRun.self) 

任何想法 – 我错过了明显的东西? 从WWDC会议和互操作指南 ,我被引导认为,“斯威夫特只是照顾这个”。

根据开发论坛 ,这个function在6.1中实现。

你所有的样本行都可以无误地编译,并且在最新的Xcode 6.1(6A1052c)中按预期工作。

与CF对象的互操作性很重要。 你可能会发现另一个问题,在这种情况下,它会被报告为错误。