How can I get all the file names of documents in my sandbox?
I have the following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
However I am clueless as to how to get all the files names and assign them to an array?
Any help would be appr开发者_StackOverfloweciated.
NSFileManager has a method called contentsOfDirectoryAtPath:error:
that returns an array of all the files in that directory.
You can use it like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if([paths count] > 0)
{
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError *error = nil;
NSArray *documentArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
if(error)
{
NSLog(@"Could not get list of documents in directory, error = %@",error);
}
}
The documentsArray
object will contain a list of all the files in that directory.
Use NSFileManager's contentsOfDirectoryAtPath:error:
method.
NSError *error;
NSFileManager* fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSArray* documentsArray = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
[documentsArray count];
Is another way of doing this for those with a different set up.
精彩评论