Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert NSURL to CFURL in swift?

Tags:

ios

swift

Here is the code I am running in the Swift playground:

import Foundation
import AudioToolbox

var audioURL:NSURL = NSURL.fileURLWithPath("/path")

var audioFile:UnsafePointer<AudioFileID>

var audioCfUrl:CFURL = audioURL as CFURL

AudioFileOpenURL(audioCfUrl!, Int8(kAudioFileReadPermission), 0, &audioFile)

On the last line I am getting the error:

'NSURL' is not a subtype of CFURL
like image 544
Dmitry Klochkov Avatar asked Aug 12 '14 19:08

Dmitry Klochkov


1 Answers

The error message might be misleading.

  • audioCfUrl! is wrong because audioCfUrl is not an optional.
  • The last argument should be the address of an AudioFileID variable.

As already said in a (now deleted) answer, you don't have to cast the NSURL to CFURL:

let audioURL = NSURL.fileURLWithPath("/path")
var audioFile : AudioFileID = nil
let status = AudioFileOpenURL(audioURL, Int8(kAudioFileReadPermission), 0, &audioFile)
like image 128
Martin R Avatar answered Oct 23 '22 03:10

Martin R