2017-01-11 12 views
0

私のアプリでは、オーディオを録音することができます(のように音声メモ)。録音が終了すると、ユーザからの入力を受けてレコードに名前を与え、オーディオはUITableViewに表示されます。録音されたオーディオは名前でソートされます(アルファベット順)。作成日によってソートする必要があります。最後に作成されたオーディオが最初に表示されます。私は2つの配列を使用しました -Swift - テーブルビューのセルを作成日でソート

1.recordedAudioFilesURLArray(タイプ:URL)& 2.recordedAudioFileName(タイプ:文字列)。

録音されたオーディオは、ドキュメントディレクトリに保存されます。ここに私のコードサンプルがある...

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はrecordedAudioFilesURLArrayを宣言しました)、ファイル情報辞書にもう1つのキーを追加します' let fileInfo = ["filepath":item、 "name":fileName !, "createnDate":creationDate!] as [String :any] 'と再生したいときは' recordedAudioFilesURLArray 'の代わりに' temprecordedAudioFilesArray'を参照してください。 –

+0

も ​​'recordedAudioFilesURLArray'をローカル変数として作成し、代わりに' temprecordedAudioFilesArray 'を使用します。 'temprecordedAudioFilesArray'は 'URL'の配列ですが、' temprecordedAudioFilesArray'は辞書の配列です。 –

関連する問題