我正在尝试将CoreAnimationText示例的元素移植到Swift。我无法弄清楚,如何从数组中提取或向下转换CTRun的元素,以便将它们传递给期望并作用于Swift-ified CTRun类型的函数。我要么得到运行时错误,要么从下面的游乐场代码段中链接错误
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会议和interop guide开始,我被引导相信“Swift只是照顾这个”。
答案 0 :(得分:0)
根据dev forums,此功能在6.1中实现。
所有示例行都编译没有错误,并按预期工作 最新的Xcode,6.1(6A1052c)。
与CF对象的互操作性正在推动。你可能会发现另一个 在这种情况下,它会被报告为错误。