PaolaJ. PaolaJ. - 13 days ago 6
iOS Question

How to filter array and let stay only strings end with .png

I am reading files from folder, folder contains

.pngs
but I get additionally
Thumb.db
file in every folder and all others are .pngs. I read content as

NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];


How to remove all files which contains
.db
in array or to stay just which ends with
.png
?

Answer

I think by using NSPredicate method you can get exact values

NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
NSString *str              = @".png"
NSPredicate *sPredicate    = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",str];
NSArray *predicateArray    = [directoryContent filteredArrayUsingPredicate:sPredicate];
Comments