如何在xcode 4.2中的Supporting Files文件夹中获取文件?

时间:2012-06-29 10:28:17

标签: iphone filesystems xcode4.2 nsbundle

我已完成以下步骤。 1.创建了一个xcode项目,并在Supporting Files中创建了一个名为“Images”的文件夹。

  1. 将4张图片拖放到其中。试图使用以下代码访问这些文件

    NSString *pathString = [[NSBundle mainBundle] pathForResource:@"Images" ofType:nil];
    NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathString error: nil];
    NSLog(@"the fileList is %d",[fileList count]);
    
  2. 计数始终为0.我现在该怎么办?里面有文件,我可以在图像视图中使用它。

    那我犯的错是什么?

2 个答案:

答案 0 :(得分:7)

Xcode不会在您的应用包中生成与群组对应的任何文件夹。添加的任何资源都存在于目标的Build Phases中的Copy Bundle Resources中。然后,您可以在[[NSBundle mainBundle] bundlePath]路径下访问您的资源。

但是如果你想计算任何文件夹下的文件,那么在添加该文件夹时你必须检查选项:

“为任何添加的文件夹创建文件夹引用。”

这将在您添加它们的同一层次结构中创建文件夹和子文件夹。然后您可以像上面那样轻松地计算它们...

否则,应用程序包将您的所有资源放在一个不在任何文件夹中的地方,就像您说的“图像”一样。使用以下代码:

NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] resourcePath] error: nil];
NSLog(@"the fileList is %d",[fileList count]);

它会列出您的所有资源。

答案 1 :(得分:0)

试试这个,

NSString *imagespath = [[NSBundle mainBundle] pathForResource:"yourimagename" ofType:@"png" inDirectory:@"images"];