获取所有文件名以Resource文件夹中的前缀开头

xyd*_*dev 10 iphone objective-c

我们如何从资源文件夹中获取以前缀开头的所有文件名.

vfn*_*vfn 22

您可以实现以下代码:

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

运行此代码,您将看到捆绑包上的PNG列表.更改谓词以匹配您想要的前缀:

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


Fat*_*tie 1

2023年

获取所有文件...

let ff = Bundle.main.urls(forResourcesWithExtension: "png", subdirectory: nil)!
Run Code Online (Sandbox Code Playgroud)

只有 png(比如)...

let ff = Bundle.main.urls(forResourcesWithExtension: "png", subdirectory: nil)!
          .compactMap{ $0.lastPathComponent }
Run Code Online (Sandbox Code Playgroud)

以“简”开头的

let ff = Bundle.main.urls(forResourcesWithExtension: "png", subdirectory: nil)!
         .compactMap{ $0.lastPathComponent }
         .filter{ $0.starts(with: "jane")}

for f in ff {  print("filename .. \(f)") }
Run Code Online (Sandbox Code Playgroud)