-5

I have NSMutableArray with music files objects in .mp3 format. All items fetched from document directory. My problem is I am getting .sqlite files along with .mp3 files. I want only .mp3 files from NSMutableArray. I tried with NSPredicate but it giving me error of Incompatible pointer types NSMutableArray/NSArray.(NSMutableArray and NSPredicate filtering)

I don't want files like (projectname.sqlite,projectname.sqlite-shm,projectname.sqlite-val etc)

My code is,

downloadedFilesArray = [[NSMutableArray alloc] init];

fileManger = [NSFileManager defaultManager];
NSError *error;
downloadedFilesArray = [[fileManger contentsOfDirectoryAtPath:fileDest error:&error] mutableCopy];

if([downloadedFilesArray containsObject:@".DS_Store"])
    [downloadedFilesArray removeObject:@".DS_Store"];

How to filter files like .mp3,.m3u,.aac??

Thanks

Community
  • 1
  • 1
Gajendra K Chauhan
  • 3,387
  • 7
  • 40
  • 55

4 Answers4

1

Use NSString's pathExtension to get the file extension, if any, or use an NSPredicate. Also, remember to check if there is an NSError when retrieving the contents of the directory.

NSMutableArray *mp3DownloadedFilesArray = [[NSMutableArray alloc] init];
NSString *targetFileExtension = @"mp3";

NSError *error;
NSArray *allDownloadedFilesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:fileDest error:&error];

if (allDownloadedFilesArray && allDownloadedFilesArray.count > 0 && !error) {
    for (NSString* fileName in allDownloadedFilesArray) {
        if ([fileName.pathExtension compare:targetFileExtension options:NSCaseInsensitiveSearch] == NSOrderedSame) {
            [mp3DownloadedFilesArray addObject:fileName];
        }
    }
} else {
    // Process the error
}
Luis Valdés
  • 1,409
  • 1
  • 12
  • 12
1

Simply use filterUsingPredicate: method:

[downloadedFilesArray filterUsingPredicate:[NSPredicate predicateWithFormat:@"SELF ENDSWITH %@", @".mp3"]];

Or for case-insensitive version:

[downloadedFilesArray filterUsingPredicate:[NSPredicate predicateWithFormat:@"SELF ENDSWITH [cd] %@", @".mp3"]];

EDIT: if you want to filter with more file extension, you can create multiple NSPredicates (one for each extension) and combine them using [NSCompoundPredicate orPredicateWithSubpredicates]. Sorry but I won't provide the ready-to-use solution, you have to implement it yourself.

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
0
NSArray *downloadedFilesArray = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil]; 

// filter the array for only sqlite files  

NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.mp3' || "]; 
NSArray *sqliteFiles = [allFiles filteredArrayUsingPredicate:fltr];
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
mandeep-dhiman
  • 2,505
  • 2
  • 21
  • 31
-1

Anyway, the NSPredicate should work, but you can write the filtering logic by yourself:

NSMutableArray* downloadedFilesArray = [[NSMutableArray alloc] init];
NSMutableArray* removeFilesArray = [[NSMutableArray alloc] init];
for (NSString* fileName in downloadedFilesArray){
    if ([fileName.lowercaseString rangeOfString:@".mp3"].location != fileName.length - 4) {
         [removeFilesArray addObject:fileName];
    }
}
[downloadedFilesArray removeObjectsInArray:removeFilesArray];
Balazs Nemeth
  • 2,333
  • 19
  • 29