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

1

0

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".

Schoolgirl answered 1/7, 2014 at 21:3 Comment(0)
S
0

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.

Schoolgirl answered 20/10, 2014 at 4:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.