How to convert a swift String to CFString

loopmasta picture loopmasta · Jun 6, 2014 · Viewed 22.3k times · Source

How can i create a CFString from a native swift String or NSString in swift

    let path:String = NSBundle.mainBundle().pathForResource(name.stringByDeletingPathExtension, ofType:"pdf")
    let string:CFString = ??? path
    let url:CFURLRef = CFURLCreateWithFileSystemPath(allocator:kCFAllocatorDefault, filePath:string, pathStyle:CFURLPathStyle.CFURLPOSIXPathStyle, isDirectory:false)

Answer

MagerValp picture MagerValp · Jun 6, 2014

Just cast it:

var str = "Hello, playground" as CFString
NSString(format: "type id: %d", CFGetTypeID(str))

Note that you'll need import Foundation for cast as CFString to work.
Otherwise if you only have import CoreFoundation, you'll need to force cast as! CFString.