Last active
April 13, 2019 17:42
-
-
Save danielctull/e0e9e5b6b942647414a4 to your computer and use it in GitHub Desktop.
Is there a better way of coercing CFArray to a swift array?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
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
Yes. Just do
cfArray as! Array<T>where T is the type that CFArray contains.