0

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)


RG_tableview array image

Community
  • 1
  • 1
richg
  • 5
  • 3

1 Answers1

0

filteredArrayUsingPredicate: returns a new array instance; it does not modify the array it is given. It is not an NSMutableArray. (The naming follows the pattern in the method -sortedArrayUsingSelector:, which returns a new array; the NSMutableArray method is -sortUsingSelector:, with a void return value -- one is describing the return value, the latter is the action to modify the receiver).

So...

_audioFileArray = [_audioFileArray filteredArrayUsingPredicate:predicate];

The second problem is that the -pathExtension method returns a string not including the extension separator itself -- so just "caf" in your case.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pathExtension='caf'"];

After that it should work. Note that -contentsOfDirectoryAtPath: returns the NSString filenames; -contentsOfDirectoryAtURL: returns full file:// NSURL instances.

Carl Lindberg
  • 2,902
  • 18
  • 22
  • Carl. Thanks for your response and suggestions, and also sorry for not updating my reply before now. Busy work schedule & other stuff etc.. – richg Oct 11 '16 at 23:41
  • I ended up using predicate class method with: _audioFileArray = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error]; //NSPredicate *predicate = [NSPredicate predicateWithFormat:<#(NSString *)#> argumentArray:<#(NSArray *)#>; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF ENDSWITH [cd] %@", @".caf"]; _filteredArray = [_audioFileArray filteredArrayUsingPredicate:predicate]; – richg Oct 11 '16 at 23:52
  • This returns my array with just the filtered .caf files. The only thing, is that I will need to be able (or let the user) edit the files held in the iOS documents folder. So i will still need the filtered array to be that of an NSMutableArray, and not just NSArray, right. Anyway, you helped me get this far. Much appreciated. – richg Oct 11 '16 at 23:53
  • Yep, that works fine too, unless you are dealing with an odd filesystem or system convention which does not use "." for the extension separator (unlikely). For mutable, just call -mutableCopy on the result to get a mutable array (or use [NSMutableArray arrayWithArray:immutableArray], which will work even if immutableArray is nil). – Carl Lindberg Oct 13 '16 at 19:10
  • Carl, thanks again. I will follow up on this. And hope you earn a badge for this thread, it was confounding me for some time. – richg Oct 15 '16 at 02:51