Dmitry Klochkov
Dmitry Klochkov

Reputation: 2635

How to convert NSURL to CFURL in 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

Upvotes: 6

Views: 8518

Answers (1)

Martin R
Martin R

Reputation: 539745

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)

Upvotes: 6

Related Questions