سؤال

How can i read all files list or array inside a directory. This directory/folder is inside my IOS app I check the below code but it returning me null.

NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"mag1"];

NSArray *myArray=[fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
NSLog(@"mag1 directory: %@",myArray);

here mag1 is my directory which contains some files. This mag1 is inside my app directory.

هل كانت مفيدة؟

المحلول

Get the top-level app bundle folder using [[NSBundle mainBundle] bundlePath]:

NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *directory = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"mag1"];

NSArray *files=[fileMgr contentsOfDirectoryAtPath:directory error:&error];
NSLog(@"mag1 directory: %@",files);

نصائح أخرى

Check the below code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingString:@"/<folder_name>"];
NSError *err = nil;
 NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(@"list %@",[fileManager contentsOfDirectoryAtPath:documentsDirectory error:&err]);

Hi you can try this one

NSMutableArray *arrayValue = [[NSMutableArray alloc]init];
NSFileManager *fManager = [NSFileManager defaultManager];
NSString *itemStr;
NSArray *array = [fManager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"mag1"] error:nil];

// Add item to an array
for (itemStr in array){
    if ([[itemStr pathExtension] isEqualToString:extension]) {
        [arrayValue addObject:item];
    }
}


NSLog(@"Array value =%@",arrayValue);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top