计算Objective C(Cocoa)中文件夹内的文件数

Yad*_*esh 8 image objective-c

我正在创建一个像闪烁书动画一样的图像数组,我将这些图像存储在一个文件夹中,实习生位于xcode中我的项目的Resource文件夹中.这些图像会有所不同,这就是我必须确定的确切数量文件夹里面的图像所以我应该如何确定?在可可有任何API来实现这一目标吗?

Cha*_*nok 15

尝试

int paths = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/your/path/here" error:NIL] count];
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请访问 http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html


Jha*_*iya 10

首先,您需要访问应用程序的包路径:

 NSMutableString* bundlePath = [NSMutableString stringWithCapacity:4];
 [bundlePath appendString:[[NSBundle mainBundle] bundlePath]];
Run Code Online (Sandbox Code Playgroud)

现在将您的文件夹名称附加到bundlePath

 [bundlePath appendString:@"/MyFolder"];
 NSArray *directoryContent  = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundlePath error:nil];
 int numberOfFileInFolder = [directoryContent count];
Run Code Online (Sandbox Code Playgroud)