如何列出所有文件夹及其子目录

如何列出所有文件夹及其子目录

本文介绍了如何列出所有文件夹及其子目录/文件在iPhone SDK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户选择她/他的iPhone中存在的任何文件,以便用作电子邮件附件。为此,我想显示iPhone中的所有文件和文件夹的列表。但是如何获得这些文件的列表?有没有办法获得该列表?

I want the user to select any file present in her/his iPhone so that it’s used as an e-mail attachment. For this purpose, I want to show the list of all files and folders present in iPhone. But how would I get the list of those files? Is there any way to get that list?

推荐答案

此代码假设您可以访问文件和文件夹。考虑到您的应用在沙盒中运行,并且无法在沙箱外获取任何文件夹/文件。

This code assumes that you have access to the files and folders. Take into account that your app runs in a sandbox and would not be able to get any folder/file outside of that sandbox.

ObjectiveC / p>

ObjectiveC

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
for (NSString *s in fileList){
    NSLog(@"%@", s);
}

Swift

let documentsDirectory =  try! NSFileManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
let fileEnumerator = NSFileManager.defaultManager().enumeratorAtURL(documentsDirectory, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions(), errorHandler: nil)
while let file = fileEnumerator?.nextObject() {
    print(file)
}

这篇关于如何列出所有文件夹及其子目录/文件在iPhone SDK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 23:36