Skip to content

Instantly share code, notes, and snippets.

@danielctull
Last active April 13, 2019 17:42
Show Gist options
  • Save danielctull/e0e9e5b6b942647414a4 to your computer and use it in GitHub Desktop.
Save danielctull/e0e9e5b6b942647414a4 to your computer and use it in GitHub Desktop.
Is there a better way of coercing CFArray to a swift array?
import CoreText
extension CTFrame {
var lines: [CTLine] {
// This is the only way I've currently found to get the
// CFArray of CTLines to cast to a swift array.
let linesAO: [AnyObject] = CTFrameGetLines(self) as [AnyObject]
guard let lines = linesAO as? [CTLine] else {
return []
}
return lines
}
}
// This is the code I have that uses the code above.
// It doesn't work in a playground though for some reason.
import CoreText
extension CTFrame {
var lines: [CTLine] {
// This is the only way I've currently found to get the
// CFArray of CTLines to cast to a swift array.
let linesAO: [AnyObject] = CTFrameGetLines(self) as [AnyObject]
guard let lines = linesAO as? [CTLine] else {
return []
}
return lines
}
}
let attributes: [String : AnyObject] = [
String(kCTFontAttributeName): CTFontCreateWithName("HelveticaNeue", 40, nil)
]
let text = "Hello World"
let rect = CGRect(origin: CGPoint(), size: CGSize(width: 120, height: 500))
let string = CFAttributedStringCreate(nil, text, nil)
let framesetter = CTFramesetterCreateWithAttributedString(string)
let framePath = CGPathCreateWithRect(rect, nil)
let frame = CTFramesetterCreateFrame(framesetter, CFRange(), framePath, nil)
let lines = frame.lines
@BertrandMPT
Copy link

Hello Bryan,

I began using Swift one week ago and doesn't understand how to use.
I think I can get the list of network interfaces of my Mac using "let netInterfaces = SCNetworkInterfaceCopyAll()" but this give a CFArray.
I can't use this CFArray in a for loop as "for netInterface in netInterfaces".

Can you explain how to use your advise ?

Thanks
Bertrand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment