iOS Newbie iOS Newbie - 2 months ago 31
iOS Question

FileManager and urlsForDirectory Error in Swift 3 Xcode 8

I just went to Xcode 8 beta 4 from beta 3 but for what ever reason I am getting this error that has worked with no errors or issued until beta 4. here is the error"

Value of type 'FileManager' has no member 'urlsForDirectory'
"

my code:

func setupRecorder(){

let audioSession:AVAudioSession = AVAudioSession.sharedInstance()

//ask for permission
if (audioSession.responds(to: #selector(AVAudioSession.requestRecordPermission(_:)))) {
AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
if granted {
print("Permission Granted\n")

//set category and activate recorder session
do {
//----
let fileManager = FileManager.default
let urls = fileManager.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)
//----
let documentDirectory = urls[0] as URL
let soundURL = try! documentDirectory.appendingPathComponent("sound.m4a")

try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)

try self.soundRecorder = AVAudioRecorder(url: soundURL, settings: self.recordSettings)

let micInput = try AVAudioRecorder(url
: soundURL, settings: self.recordSettings)


self.soundRecorder.prepareToRecord()
self.soundRecorder.updateMeters()



} catch {

print("Error Recording");

}

}
})
}

}


BUT the only part is this line:

let urls = fileManager.urlsForDirectory(.documentDirectory, inDomains: .userDomainMask)


"
'Value of type 'FileManager' has no member 'urlsForDirectory'
"

Answer

You just need to change it to urls(for:) as follow:

let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
Comments