如何检查文件夹是否为空,并将文件夹中的文件名实例化为NSStrings?(iphone可可)

Jar*_*Loo 7 iphone objective-c nsfilemanager

只是想知道,我如何检查特定文件夹是否正在保存文件,并将文件夹中的文件名实例化为NSStrings?我知道一个名为NSFileManager的类,但我不确定如何应用它来满足我的目标.

SVD*_*SVD 7

NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderLocationString error:nil];
Run Code Online (Sandbox Code Playgroud)


Mad*_*dhu 5

默认情况下,所有自定义文件和数据都将存储在应用程序的文档目录中.我在下面放了一个示例代码来访问默认文档目录; 加上一个名为'MyFolderName'的自定义文件夹

最终结果将是一个数组,其中包含您指定的路径中的文件或目录的NSString对象列表.

//Accessing the default documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//Appending the name of your custom folder, if you have any
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"MyFolderName"]; 

NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:path]) { // Directory exists
NSArray *listOfFiles = [fileManager contentsOfDirectoryAtPath:path error:nil];
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!:)