2017-01-11 115 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是字典數組。 –