2017-01-11 117 views
0

在我的应用程序中,用户可以录制音频(如语音备忘录)。完成记录后,需要用户输入记录名称,并在UITableView中显示音频。录制的音频按其名称排序(按字母顺序)。我需要按创建日期对它们进行排序 - 最后创建的音频将首先出现。我使用了两个阵列 -Swift - 按创建日期排序表单元格

1.recordedAudioFilesURLArray(Type:URL)& 2.recordedAudioFileName(Type:String)。

录制的音频保存在文档目录中。这里是我的代码示例...

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 
} 
+0

你有音频的日期在一个阵列?如果是,则共享示例日期数组。 –

+0

不...兄弟。我没有 –

+2

如果你没有,你愿意根据日期排序吗? –

回答

0

这个stackoverflow answer显示了如何使用NSFileManager API获取文件创建日期。

使用上面的答案,我试过了一个样本。

//This array will hold info like filename and creation date. You can choose to create model class for this 
    var fileArray = [[String:NSObject]]() 

    //traverse each file in the array 
    for path in recordedAudioFilesURLArray! 
    { 
     //get metadata (attibutes) for each file 
     let dictionary = try? NSFileManager.defaultManager().attributesOfItemAtPath(path.path!) 

     //save creationDate for each file, we will need this to sort it 
     let fileDictionary = ["fileName":path.lastPathComponent!, NSFileCreationDate:dictionary?[NSFileCreationDate] as! NSDate] 
     fileArray.append(fileDictionary) 
    } 

    //sorting goes here 
    fileArray.sortInPlace { (obj1, obj2) -> Bool in 

     let date1 = obj1[NSFileCreationDate] as! NSDate 
     let date2 = obj2[NSFileCreationDate] as! NSDate 

     return (date2.compare(date1) == .OrderedDescending) 
    } 

    //Let's check the result 
    for dictionary in fileArray 
    { 
     NSLog("\(dictionary["fileName"])") 
    } 

这对我有用。希望有所帮助。

注意:这只是我试过的一个样本。您可能需要对 进行一些修改以适合您的情况。

0

尝试下面的代码 FUNC getRecordedAudioFilesFromDocDirectory(){VAR temprecordedAudioFilesArray:[NSDictionary的] = []

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 == "mp3" } 

    } catch let error as NSError { 
     print(error.localizedDescription) 
    } 
    for item in recordedAudioFilesURLArray { 
     var fileName: String? 
     var creationDate : Date? 
     let path: String = item.path 
     do{ 
      let attr = try FileManager.default.attributesOfItem(atPath: path) 
      creationDate = attr[FileAttributeKey.creationDate] as? Date 
      fileName = item.lastPathComponent 

      let fileInfo = ["filepath": item, "name": fileName!, "createnDate": creationDate!] 
      temprecordedAudioFilesArray.append(fileInfo as NSDictionary) 


     } 
     catch { 

     } 

    } 
    temprecordedAudioFilesArray.sort(by: { (($0 as! Dictionary<String, AnyObject>)["createnDate"] as? NSDate)?.compare(($1 as! Dictionary<String, AnyObject>)["createnDate"] as? NSDate as! Date) == .orderedAscending}) 

    for file in temprecordedAudioFilesArray{ 
     recordedAudioFileNames.append((file["name"] as? String)!) 
     print(file["name"]) 
    } 

} 
+0

** temprecordedAudioFilesArray **中的元素完美排序来获取文件创建日期。但根据这个我需要排序** recordedAudioFilesURLArray **,因为这个数组包含记录声音的URL,需要播放它们。怎么做?你能帮我吗 ? –

+0

声明'temprecordedAudioFilesArray'外部函数(其中u声明了recordingAudioFilesURLArray),并在文件信息词典中增加了一个密钥'let fileInfo = [“filepath”:item,“name”:fileName !,“createnDate”:creationDate!] as [String :任何]'当你想玩,请参考'temprecordedAudioFilesArray'而不是'recordedAudioFilesURLArray'希望这有助于 –

+0

也'使'recordingAudioFilesURLArray'作为本地变量,并使用'temprecordedAudioFilesArray'代替它。您需要对此进行代码更改,因为temprecordedAudioFilesArray是URL的数组,但temprecordedAudioFilesArray是字典数组。 –