Swift: Extracting / downcasting CFType based CoreText types in a CFArray

杀马特。学长 韩版系。学妹 提交于 2019-12-02 19:36:20

问题


I am trying to port elements of the CoreAnimationText sample to Swift. I cannot figure out though, how to extract or downcast the elements of CTRun from an array, in order to pass them to functions that expect and act upon the Swift-ified CTRun type. I either get runtime errors or linking errors from the playground snippet below

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)

Any ideas - am I missing something obvious? From the WWDC sessions and interop guide, I am led to believe that "Swift just takes care of this".


回答1:


According to dev forums, this functionality is implemented in 6.1.

All your sample lines compile without errors and work as expected in the latest Xcode, 6.1 (6A1052c) .

Interoperability with CF objects is impoving. You may find another issues, in such case it'd be reported as bug.



来源:https://stackoverflow.com/questions/24519235/swift-extracting-downcasting-cftype-based-coretext-types-in-a-cfarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!