In my app user can record audio (Like Voice Memos). After finishing record, it takes input from the user to give the record a name and audios are shown in UITableView
. Recorded audios are sorted by their name (Alphabetically). I need to sort them by creation date - last created audio will be appeared first. I used two arrays -
1.recordedAudioFilesURLArray (Type: URL) & 2.recordedAudioFileName (Type: String).
Recorded audios are saved in document directory. Here is my code sample...
func getRecordedAudioFilesFromDocDirectory() {
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
let directoryContents = try FileManager.default.contentsOfDirectory( at: documentsUrl, includingPropertiesForKeys: nil, options: [])
recordedAudioFilesURLArray = directoryContents.filter{ $0.pathExtension == "m4a" }
} catch let error as NSError {
print(error.localizedDescription)
}
recordedAudioFileNames = recordedAudioFilesURLArray.flatMap({$0.deletingPathExtension().lastPathComponent})
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return recordedAudioFilesURLArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = recordedAudioFileNames[indexPath.row] as! NSString as String
return cell
}