Get all file names starting with a prefix from Resource folder

前端 未结 2 675
生来不讨喜
生来不讨喜 2021-02-08 13:02

How can we get all file names starting with a prefix from resource folder..

2条回答
  •  醉话见心
    2021-02-08 14:03

    You can achieve that adapting the following code:

    NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
                      [[NSBundle mainBundle] bundlePath] error:nil];
    NSArray *pngs = [files filteredArrayUsingPredicate:
                     [NSPredicate predicateWithFormat:@"self ENDSWITH[cd] '.png'"]];
    NSLog(@"%@", pngs);
    

    Running this code you will see the list of PNG's on your bundle. Change the predicate to match with the prefix you want:

    NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
                      [[NSBundle mainBundle] bundlePath] error:nil];
    NSArray *filesWithSelectedPrefix = [files filteredArrayUsingPredicate:
                                        [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] 'prefix'"]];
    NSLog(@"%@", filesWithSelectedPrefix);
    

提交回复
热议问题