My problem: I have already searched this forum regarding this topic (i.e.:Getting a list of files in a directory with a glob), but still having some challenges.
Using Objective-C, I am populating a tableview with an array of files stored in my iOS root “documents directory”.
The files are .caf audio files (i.e.: audio_1.caf, audio_2.caf etc). I am using the NSArray _audioFileArray to list the files in the UITableview cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"simpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:simpleTableIdentifier];
}
//add the text display that is drawn from the array list
cell.textLabel.text = [_audioFileArray objectAtIndex:indexPath.row];
//cell.textLabel.text = [NSString stringWithFormat:@"%@.!!", _audioFileArray];
return cell;
}
The console print out this array is:
2016-09-25 18:27:08.278 NewAudioPlayerTest[3349:60b] AudioFileArray check:
-
(
".DS_Store",
"audio 1.caf",
"audio 10.caf",
"audio 2.caf",
"audio 3.caf",
"audio 4.caf",
"audio 5.caf",
"audio 6.caf",
"audio 7.caf",
"audio 8.caf",
"audio 9.caf" )
Note also showing up in the NSLog of the populated tableview is the .DS_Store invisible file, which I don’t want, just need the audio files for future play back.
I have tried using the predicate filter with no success, and using the “NSDirectoryEnumerationSkipsHiddenFiles” method with some results. If I NSLog for the results of this method, as an array, I get a mangled version of the file and extension, along with the whole URL in the console, as shown below…
**"file:///Users/aName/Library/Developer/CoreSimulator/Devices/D144B87B-F1A6-460E-9628-5C203396B60D/data/Applications/C85E57DD-E183-48C5-8D64-F9C497E9AA2E/Documents/audio%201.caf",
"file:///Users/aName/Library/Developer/CoreSimulator/Devices/D144B87B-F1A6-460E-9628-5C203396B60D/data/Applications/C85E57DD-E183-48C5-8D64-F9C497E9AA2E/Documents/audio%202.caf",**
..and so on.
The bulk of the code that is dealing with all of this in my DetailViewController.m file is:
@interface FileViewController ()
@end
@implementation FileViewController
int count;
@synthesize fileArray = _fileArray;
@synthesize fileName = _fileName;
@synthesize audioFileArray = _audioFileArray;
@synthesize contents = _contents;
@synthesize fileURL = _fileURL;
- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager *fileManager = [NSFileManager defaultManager];
_audioFileArray = [[NSMutableArray alloc] init];
_fileName = [NSString stringWithFormat:@"audio %d", count];
NSArray *paths = [NSArray arrayWithObjects: [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject],
_fileName,
nil];
NSString *documentsDirectory = [paths objectAtIndex:0];
// WORKS with this, but whole mangled URL ——>
_fileURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",[paths objectAtIndex:0]]];
NSError *error = nil;
///—> not working!!
_audioFileArray = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pathExtension='.caf'"];
[_audioFileArray filteredArrayUsingPredicate:predicate];
_contents = [fileManager contentsOfDirectoryAtURL:_fileURL includingPropertiesForKeys: [NSArray arrayWithObject:NSURLNameKey] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
NSArray *theFiles = [fileManager contentsOfDirectoryAtURL:[NSURL fileURLWithPath:[paths objectAtIndex:0]]
includingPropertiesForKeys:[NSArray arrayWithObject:NSURLNameKey]
options:NSDirectoryEnumerationSkipsHiddenFiles
error:nil];
NSLog(@“skipHiddenFiles log ---> %@“,_contents);
NSLog(@"now predicate ---> %@",predicate);
This code may look a bit of a mess, (yep, sorry for that) What I of course want is use the NSArray _audioFileArray instance method that has the hidden files filtered out. Welcome to any solutions and suggestions on which is the best way to approach this and make the filtering work, and what I'm getting wrong here.
Thank you. (tableView list of my array posted in pic below)