在Objective-C中的指定路径获取具有特定扩展名的文件列表 [英] Get list of files with specific extensions at a specified path in Objective-C
问题描述
我对Objective-C很新。我正在开发一个Cocoa应用程序。
目前我在Objective C中寻找这个C#代码:
string [] fileList = Directory .GetFiles(DownloadPath,* .jpg);
返回的字符串不一定是完整路径,因为我需要的是文件名。我试过NSFileManager,但到目前为止没有好的。谢谢。
编辑:我尝试过的NSFileManager:
[someFileManager contentsOfDirectoryAtPath:path error:nil];
我还想问:'path'的格式是什么?这听起来很容易,但我完全没有MAC OS文件系统。我使用的路径是从[NSOpenPanel URL],它们看起来像这样:
file:// localhost /有时我得到结果,但有一些其他时间返回的NSArray只是一个简单的例子。空。我很困惑这一切,所以任何帮助将不胜感激。
EDIT2:
这里的答案: NSPredicate结束多个文件,可能是一个更好的选择。
解决方案此代码应该可以运行:
NSArray * dirFiles = [[NSFileManager defaultManager] contentOfDirectoryAtPath:path error:nil];
NSArray * jpgFiles = [dirFiles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@self ENDSWITH'.jpg']];
I'm pretty new to Objective-C. I'm developing a Cocoa application.
Currently I'm looking for the equivalent of this C# code in Objective C:
string[] fileList = Directory.GetFiles(DownloadPath, "*.jpg");
The returned strings need not necessarily be full path, since all I need is the file names. I have tried NSFileManager but so far no good yet. Thank you.
EDIT: What I've tried with NSFileManager:
[someFileManager contentsOfDirectoryAtPath:path error:nil];
I also want to ask: what is the format of 'path'? This sounds easy, but I'm completely clueless about MAC OS file system. The path that I'm using is from [NSOpenPanel URLs], and they looks like this:
file://localhost/Users/alex/Movies/
Sometimes I get the results, but some other time The returned NSArray is just empty. I'm pretty confused about this so any help would be appreciated.
EDIT2:
The answer here: NSPredicate endswith multiple files, is probably a better choice. Nevertheless, thank you for your answer.
解决方案 This code should work:
NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSArray *jpgFiles = [dirFiles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"]];
这篇关于在Objective-C中的指定路径获取具有特定扩展名的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!