获取Objective-C中指定路径的特定扩展名的文件列表

Zek*_*jin 24 cocoa objective-c

我对Objective-C很新.我正在开发一个Cocoa应用程序.目前我正在寻找Objective C中这个C#代码的等价物:

string[] fileList = Directory.GetFiles(DownloadPath, "*.jpg");
Run Code Online (Sandbox Code Playgroud)

返回的字符串不一定是完整路径,因为我需要的只是文件名.我已经尝试过NSFileManager但到目前为止还没有好的.谢谢.

编辑:我尝试过使用NSFileManager:

[someFileManager contentsOfDirectoryAtPath:path error:nil];
Run Code Online (Sandbox Code Playgroud)

我也想问:'path'的格式是什么?这听起来很简单,但我对MAC OS文件系统完全无能为力.我使用的路径来自[NSOpenPanel URL],它们看起来像这样:

file://localhost/Users/alex/Movies/
Run Code Online (Sandbox Code Playgroud)

有时我会得到结果,但有些时候返回的NSArray只是空的.我对此非常困惑,所以任何帮助都会受到赞赏.

EDIT2:答案在这里:NSPredicate以多个文件结尾,可能是更好的选择.不过,谢谢你的回答.

Ven*_*MKO 47

这段代码应该有效:

NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSArray *jpgFiles = [dirFiles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"]];
Run Code Online (Sandbox Code Playgroud)