How do I get an array of file names from the iOS File Manager












0















I'm creating an Audio Recording app and have declared an empty array to store all the names of the files.



var recordingTitles: [String] = 


When I launch the app, I have a function that checks what files are already stored in the File Manager and returns the number of files in order to display the correct number of cells in the collectionView.



override func viewDidLoad() {
super.viewDidLoad()
getNumberOfRecordings()

}

// Get number of actual recording files stored in the File Manager Directory
func getNumberOfRecordings() {
let directoryUrl = getDirectory()

do {
contentsOfFileManager = try myFileManager.contentsOfDirectory(at: directoryUrl, includingPropertiesForKeys: [URLResourceKey(rawValue: recordingsKey)], options: .skipsHiddenFiles)

} catch let error {
print(error.localizedDescription)
}
numberOfRecordings = contentsOfFileManager.count
}


I would like to obtain the names of the files and assign them to the "recordingTitles" array in order to display the correct title for each cell. I've tried reading over the Apple docs for File Manager but haven't found a solution yet. Thank you in advance for your help!










share|improve this question























  • What output do you get right now? An empty list? An error?

    – skylerl
    Dec 31 '18 at 19:05











  • Thank you for your help @skylerl. The recordingTitles array starts out empty, and so when the app tries to load the titles based on the number of files currently in the File Manager Directory I get an "Index out of range". My getNumberOfRecordings() function goes through the process of getting an array of URLs from the File Manager, but I'm not sure how to obtain just the file names and convert them to an array of Strings to assign to the recordingTitles.

    – Daniel
    Dec 31 '18 at 19:06


















0















I'm creating an Audio Recording app and have declared an empty array to store all the names of the files.



var recordingTitles: [String] = 


When I launch the app, I have a function that checks what files are already stored in the File Manager and returns the number of files in order to display the correct number of cells in the collectionView.



override func viewDidLoad() {
super.viewDidLoad()
getNumberOfRecordings()

}

// Get number of actual recording files stored in the File Manager Directory
func getNumberOfRecordings() {
let directoryUrl = getDirectory()

do {
contentsOfFileManager = try myFileManager.contentsOfDirectory(at: directoryUrl, includingPropertiesForKeys: [URLResourceKey(rawValue: recordingsKey)], options: .skipsHiddenFiles)

} catch let error {
print(error.localizedDescription)
}
numberOfRecordings = contentsOfFileManager.count
}


I would like to obtain the names of the files and assign them to the "recordingTitles" array in order to display the correct title for each cell. I've tried reading over the Apple docs for File Manager but haven't found a solution yet. Thank you in advance for your help!










share|improve this question























  • What output do you get right now? An empty list? An error?

    – skylerl
    Dec 31 '18 at 19:05











  • Thank you for your help @skylerl. The recordingTitles array starts out empty, and so when the app tries to load the titles based on the number of files currently in the File Manager Directory I get an "Index out of range". My getNumberOfRecordings() function goes through the process of getting an array of URLs from the File Manager, but I'm not sure how to obtain just the file names and convert them to an array of Strings to assign to the recordingTitles.

    – Daniel
    Dec 31 '18 at 19:06
















0












0








0








I'm creating an Audio Recording app and have declared an empty array to store all the names of the files.



var recordingTitles: [String] = 


When I launch the app, I have a function that checks what files are already stored in the File Manager and returns the number of files in order to display the correct number of cells in the collectionView.



override func viewDidLoad() {
super.viewDidLoad()
getNumberOfRecordings()

}

// Get number of actual recording files stored in the File Manager Directory
func getNumberOfRecordings() {
let directoryUrl = getDirectory()

do {
contentsOfFileManager = try myFileManager.contentsOfDirectory(at: directoryUrl, includingPropertiesForKeys: [URLResourceKey(rawValue: recordingsKey)], options: .skipsHiddenFiles)

} catch let error {
print(error.localizedDescription)
}
numberOfRecordings = contentsOfFileManager.count
}


I would like to obtain the names of the files and assign them to the "recordingTitles" array in order to display the correct title for each cell. I've tried reading over the Apple docs for File Manager but haven't found a solution yet. Thank you in advance for your help!










share|improve this question














I'm creating an Audio Recording app and have declared an empty array to store all the names of the files.



var recordingTitles: [String] = 


When I launch the app, I have a function that checks what files are already stored in the File Manager and returns the number of files in order to display the correct number of cells in the collectionView.



override func viewDidLoad() {
super.viewDidLoad()
getNumberOfRecordings()

}

// Get number of actual recording files stored in the File Manager Directory
func getNumberOfRecordings() {
let directoryUrl = getDirectory()

do {
contentsOfFileManager = try myFileManager.contentsOfDirectory(at: directoryUrl, includingPropertiesForKeys: [URLResourceKey(rawValue: recordingsKey)], options: .skipsHiddenFiles)

} catch let error {
print(error.localizedDescription)
}
numberOfRecordings = contentsOfFileManager.count
}


I would like to obtain the names of the files and assign them to the "recordingTitles" array in order to display the correct title for each cell. I've tried reading over the Apple docs for File Manager but haven't found a solution yet. Thank you in advance for your help!







arrays swift nsfilemanager






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 31 '18 at 19:01









DanielDaniel

164




164













  • What output do you get right now? An empty list? An error?

    – skylerl
    Dec 31 '18 at 19:05











  • Thank you for your help @skylerl. The recordingTitles array starts out empty, and so when the app tries to load the titles based on the number of files currently in the File Manager Directory I get an "Index out of range". My getNumberOfRecordings() function goes through the process of getting an array of URLs from the File Manager, but I'm not sure how to obtain just the file names and convert them to an array of Strings to assign to the recordingTitles.

    – Daniel
    Dec 31 '18 at 19:06





















  • What output do you get right now? An empty list? An error?

    – skylerl
    Dec 31 '18 at 19:05











  • Thank you for your help @skylerl. The recordingTitles array starts out empty, and so when the app tries to load the titles based on the number of files currently in the File Manager Directory I get an "Index out of range". My getNumberOfRecordings() function goes through the process of getting an array of URLs from the File Manager, but I'm not sure how to obtain just the file names and convert them to an array of Strings to assign to the recordingTitles.

    – Daniel
    Dec 31 '18 at 19:06



















What output do you get right now? An empty list? An error?

– skylerl
Dec 31 '18 at 19:05





What output do you get right now? An empty list? An error?

– skylerl
Dec 31 '18 at 19:05













Thank you for your help @skylerl. The recordingTitles array starts out empty, and so when the app tries to load the titles based on the number of files currently in the File Manager Directory I get an "Index out of range". My getNumberOfRecordings() function goes through the process of getting an array of URLs from the File Manager, but I'm not sure how to obtain just the file names and convert them to an array of Strings to assign to the recordingTitles.

– Daniel
Dec 31 '18 at 19:06







Thank you for your help @skylerl. The recordingTitles array starts out empty, and so when the app tries to load the titles based on the number of files currently in the File Manager Directory I get an "Index out of range". My getNumberOfRecordings() function goes through the process of getting an array of URLs from the File Manager, but I'm not sure how to obtain just the file names and convert them to an array of Strings to assign to the recordingTitles.

– Daniel
Dec 31 '18 at 19:06














2 Answers
2






active

oldest

votes


















0














So, contentsOfFileManager is going to consist of NSURL objects. These objects have different fields you can pluck out and use. It looks like you're interested in just the filename so I'd like to draw your attention to the lastPathComponent field of NSURL. From the docs:




This property contains the last path component, unescaped using the replacingPercentEscapes(using:) method. For example, in the URL file:///path/to/file, the last path component is file.




In other words, calling myNsurl.lastPathComponent should yield you the filename. So you can loop through the array that's returned by myFileManager.contentsOfDirectory and get the lastPathComponent of each one and add them to another array which will contain just the filenames.






share|improve this answer































    0














    Use the map function. You probably want something like this:



    recordingTitles = try myFileManager.contentsOfDirectory(at:directoryURL, 
    includingPropertiesForKeys: nil).map {$0.lastPathComponent}





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53990627%2fhow-do-i-get-an-array-of-file-names-from-the-ios-file-manager%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      So, contentsOfFileManager is going to consist of NSURL objects. These objects have different fields you can pluck out and use. It looks like you're interested in just the filename so I'd like to draw your attention to the lastPathComponent field of NSURL. From the docs:




      This property contains the last path component, unescaped using the replacingPercentEscapes(using:) method. For example, in the URL file:///path/to/file, the last path component is file.




      In other words, calling myNsurl.lastPathComponent should yield you the filename. So you can loop through the array that's returned by myFileManager.contentsOfDirectory and get the lastPathComponent of each one and add them to another array which will contain just the filenames.






      share|improve this answer




























        0














        So, contentsOfFileManager is going to consist of NSURL objects. These objects have different fields you can pluck out and use. It looks like you're interested in just the filename so I'd like to draw your attention to the lastPathComponent field of NSURL. From the docs:




        This property contains the last path component, unescaped using the replacingPercentEscapes(using:) method. For example, in the URL file:///path/to/file, the last path component is file.




        In other words, calling myNsurl.lastPathComponent should yield you the filename. So you can loop through the array that's returned by myFileManager.contentsOfDirectory and get the lastPathComponent of each one and add them to another array which will contain just the filenames.






        share|improve this answer


























          0












          0








          0







          So, contentsOfFileManager is going to consist of NSURL objects. These objects have different fields you can pluck out and use. It looks like you're interested in just the filename so I'd like to draw your attention to the lastPathComponent field of NSURL. From the docs:




          This property contains the last path component, unescaped using the replacingPercentEscapes(using:) method. For example, in the URL file:///path/to/file, the last path component is file.




          In other words, calling myNsurl.lastPathComponent should yield you the filename. So you can loop through the array that's returned by myFileManager.contentsOfDirectory and get the lastPathComponent of each one and add them to another array which will contain just the filenames.






          share|improve this answer













          So, contentsOfFileManager is going to consist of NSURL objects. These objects have different fields you can pluck out and use. It looks like you're interested in just the filename so I'd like to draw your attention to the lastPathComponent field of NSURL. From the docs:




          This property contains the last path component, unescaped using the replacingPercentEscapes(using:) method. For example, in the URL file:///path/to/file, the last path component is file.




          In other words, calling myNsurl.lastPathComponent should yield you the filename. So you can loop through the array that's returned by myFileManager.contentsOfDirectory and get the lastPathComponent of each one and add them to another array which will contain just the filenames.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 31 '18 at 19:17









          skylerlskylerl

          1,463102950




          1,463102950

























              0














              Use the map function. You probably want something like this:



              recordingTitles = try myFileManager.contentsOfDirectory(at:directoryURL, 
              includingPropertiesForKeys: nil).map {$0.lastPathComponent}





              share|improve this answer




























                0














                Use the map function. You probably want something like this:



                recordingTitles = try myFileManager.contentsOfDirectory(at:directoryURL, 
                includingPropertiesForKeys: nil).map {$0.lastPathComponent}





                share|improve this answer


























                  0












                  0








                  0







                  Use the map function. You probably want something like this:



                  recordingTitles = try myFileManager.contentsOfDirectory(at:directoryURL, 
                  includingPropertiesForKeys: nil).map {$0.lastPathComponent}





                  share|improve this answer













                  Use the map function. You probably want something like this:



                  recordingTitles = try myFileManager.contentsOfDirectory(at:directoryURL, 
                  includingPropertiesForKeys: nil).map {$0.lastPathComponent}






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 31 '18 at 19:54









                  mattmatt

                  328k46530731




                  328k46530731






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53990627%2fhow-do-i-get-an-array-of-file-names-from-the-ios-file-manager%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas

                      Can't read property showImagePicker of undefined in react native iOS