iosswift2cfstring

how to retrieve CFStringEncoding in Swift other than CFStringGetSystemEncoding()?


let cStringRef : CFStringRef = CFStringCreateWithCString(nil, NSBundle.mainBundle().pathForResource("Luffy", ofType: "mp3")! , kCFStringEncodingMacRoman)

this encoding isn't working, though it is present in the Documentation.

to get MacRoman, I used CFStringGetSystemEncoding()

My question is how to retrieve a CFStringEncoding other than this.


Solution

  • If you just want to convert the path of Luffy.mp3 to CFString, use this:

    let cStringRef = NSBundle.mainBundle().pathForResource("Luffy", ofType: "mp3")! as CFString
    

    Edit: if you want to go the long way:

    let path = NSBundle.mainBundle().pathForResource("Luffy", ofType: "mp3")!
    let cString = path.cStringUsingEncoding(NSMacOSRomanStringEncoding)!
    
    let cfString = CFStringCreateWithCString(nil, cString, CFStringBuiltInEncodings.MacRoman.rawValue)