Using this answer, I would like to assign a string to a variable in my ViewDidLoad, as I will use this array to populate a table.
let downloads : String?
// Get the document directory url
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
// Get the directory contents urls (including subfolders urls)
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: [])
print(directoryContents)
// if you want to filter the directory contents you can do like this:
let mp3Files = directoryContents.filter{ $0.pathExtension == "mp3" }
//print("mp3 urls:",mp3Files)
let mp3FileNames = mp3Files.map{ $0.deletingPathExtension().lastPathComponent }
//print("mp3 list:", mp3FileNames)
let downloads = mp3FileNames
} catch {
print(error.localizedDescription)
}
First, if I print mp3FileNames without the variable, it prints. So the mp3 download extracting code seems good.
Second, when I add the variable for downloads, things break down. XCode flags that I am not using the variable elsewhere even though it reappears in my do loop: Immutable value 'cars' was never used; consider replacing with '_' or removing it
I am new to Swift. What is the proper way to set this variable with the string that will come from mp3FileNames?
Variable 'downloads' was never used; consider replacing with '_' or removing it