简体   繁体   English

在目录中搜索具有特定文件扩展名的文件,然后将它们添加到数组中,然后运行其中一个

[英]Search a directory for files with a certain file extensions and then add them to an array, then run one of them

I'm new to Objective-C and Macs in general and i'm trying to make a Random Video Player. 我是Objective-C和Mac的新手,我正在尝试制作随机视频播放器。

I made it for windows using Java but I want to make a native mac app to do it for me. 我是使用Java在Windows上制作的,但我想制作一个本地mac应用程序来替我做。 Basically the program has a directory chooser and a button, you select which directory you want to search and then click the button and a random video plays. 基本上,该程序具有目录选择器和按钮,您选择要搜索的目录,然后单击按钮,然后播放随机视频。

So what I need to know is, how do I search the specified directory for files with certain extensions (avi, mp4, mkv etc.) and then add their paths to an array ? 因此,我需要知道的是,如何在指定目录中搜​​索具有某些扩展名(avi,mp4,mkv等)的文件,然后将其路径添加到数组中? and then after the scan is finished randomly generate a number, and then open the file associated with that location in the array. 然后在扫描完成后随机生成一个数字,然后打开与数组中该位置关联的文件。 I would like to be able to search recursively through the directory. 我希望能够通过目录递归搜索。

Thanks for the help. 谢谢您的帮助。

Just an FYI : I use this for my TV Shows folder, I have a different folder for each show and then sub folders for each season of the show. 仅供参考:我将其用于“电视节目”文件夹,每个节目都有一个不同的文件夹,然后每个节目季节都有一个子文件夹。

OK, well you're probably going to want to: 好的,您可能会想要:

  1. Select a directory using an NSOpenPanel . 使用NSOpenPanel选择目录。 (Set setCanChooseDirectories and setCanChooseFiles accordingly.) (相应地设置setCanChooseDirectories和setCanChooseFiles。)

  2. Get the contents of that directory (well, URL) using an NSFileManager . 使用NSFileManager获取该目录的内容(URL)。 The enumeratorAtURL:includingPropertiesForKeys:options:errorHandler: method is probably what you're after as it'll list the contents of all sub-dirs, etc. enumeratorAtURL:includesPropertiesForKeys:options:errorHandler:方法可能就是你所追求的,因为它会列出所有子目录的内容等。

  3. Iterate over the files, putting the URLs in an NSMutableArray as you go before picking a random entry. 迭代文件,在选择随机条目之前将URL放在NSMutableArray中。

  4. Use an NSWorkspace to open the file (see the openFile method, etc.) 使用NSWorkspace打开文件(请参见openFile方法等)

Incidentally, if you're after sample code most of the Apple class reference documentation linked to above have sample code attached. 顺便提一下,如果您在示例代码之后,大多数链接到上面的Apple类参考文档都附带了示例代码。 (See the "Related sample code" section in the header of each document.) (请参阅每个文档的标题中的“相关示例代码”部分。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Searchbar搜索数组项并将其显示在tableview上? - Searchbar to search array items and displays them on a tableview? 尝试循环遍历图像并将其添加到数组中 - Trying to loop through images and add them to array 如果我在一个文件中有多个类,是否有工具可以将它们分成单独的文件? - If I have multiple classes in one file, is there a tool to make them into separate files? 如何将PhysicsBody添加到JSTileMap生成的图块并将其更改为一个physicsBody - How to add physicsBody to tiles generated by JSTileMap and change them into one physicsBody 删除数组元素并将其添加到相同的索引iOS - Remove array elements and add them at the same index iOS 从Iphone / IOS的​​网站目录中获取所有文件URL的列表,并将其存储在NSMutableArray中 - Get A List of all files's url from a Website Directory in Iphone / IOS and store them in NSMutableArray 创建文件并在其中保存对象 - Creating Files And Saving Objects In Them 如何从在线目录加载UIImage数组中的图像并将其存储在App中 - How to load images in UIImage array from an online directory and store them in the App 在它们之间一定距离处创建UIbutton - Create UIbuttons at a certain distance between them 一个控制器来全部统治 - One controller to rule them all
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM