How to convert NSURL to CFURL in swift?

Here is the code that I run on 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) 

In the last line, I get the error message:

 'NSURL' is not a subtype of CFURL 
+5
source share
1 answer

The error message may be misleading.

  • audioCfUrl! incorrect because audioCfUrl is not optional.
  • The last argument must be the address of the AudioFileID variable.

As already mentioned in the answer (now deleted), you do not need to allocate NSURL before CFURL :

 let audioURL = NSURL.fileURLWithPath("/path") var audioFile : AudioFileID = nil let status = AudioFileOpenURL(audioURL, Int8(kAudioFileReadPermission), 0, &audioFile) 
+6
source

Source: https://habr.com/ru/post/1200158/


All Articles