Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a swift String to CFString

Tags:

swift

cfstring

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) 
like image 960
loopmasta Avatar asked Jun 06 '14 20:06

loopmasta


People also ask

What is CFString in Swift?

Overview. CFString provides a suite of efficient string-manipulation and string-conversion functions. It offers seamless Unicode support and facilitates the sharing of data between Cocoa and C-based programs.

How do I convert a string to a double in Swiftui?

To convert a string to a double, we can use the built-in Double() initializer syntax in Swift. The Double() initializer takes the string as an input and returns the double instance. If the string contains invalid characters or invalid format it returns nil.


2 Answers

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.

like image 176
MagerValp Avatar answered Oct 07 '22 11:10

MagerValp


If you want to convert a non-literal string, you have to cast it to NSString.

let replacement = "World" let string = "Hello, \(replacement)"  let cfstring:CFString = string as NSString 

Swift knows how to convert a swift string to an NSString and an NSString to a CFString, but seems not to know how to do both steps in one.

like image 25
freytag Avatar answered Oct 07 '22 10:10

freytag