来自文件夹内文件名的 NSString 数组? [英] Array of NSStrings from filenames within a folder?

查看:30
本文介绍了来自文件夹内文件名的 NSString 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含我已拖入项目的文件夹内容的 NSString 数组...但是当我之后计算数组中的项目时,它总是返回 0;

所以,我的项目中的文件夹看起来像这样

-卡片-颜色蓝色.png绿色.png橙色.png黄色.png紫色.png黑色.png

而我试图获取此文件列表(颜色 png)的代码是

NSError *error = nil;NSString *pathString = [[NSString alloc] init];pathString = [[NSString alloc] initWithString:@"/Cards/Colors/"];NSArray *fileList = [[NSArray alloc] init];fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathString 错误:&error];[pathString 发布];NSLog(@"%@", 错误);//这总是 0NSLog(@"文件列表有 %i 个项目", [fileList count]);

我得到的 NSError 是

Error Domain=NSCocoaErrorDomain Code=260 操作无法完成.(Cocoa 错误 260.)" UserInfo=0x596db00 {NSUserStringVariant=(文件夹), NSFilePath=/Cards/Color/, NSUnderlyingError=0x5925ef0 "操作无法完成.没有那个文件或目录"}

知道我哪里出错了吗?

解决方案

您正在将 pathString 初始化为绝对路径 /Cards/Colors/.此路径是系统范围的路径,因此在 iPhone 上,远在您的应用沙箱之外.

试试这个:

NSString *pathString = [[NSBundle mainBundle] pathForResource:@"Cards/Colors" ofType:nil];NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathString 错误:&error];

(请注意,您在问题中使用代码的方式是分配/初始化 fileList,然后通过将 contentsOfDirectoryAtPath:error 的结果分配给该对象来立即泄漏该对象:.这是一个错误.)

I'm trying to create an array of NSStrings of the contents of a folder that I've dragged into my project... but when I count the items in the array afterwards, it's always comes back with 0;

So, my folder in my project looks like this

-Cards
  -Colors
     Blue.png
     Green.png
     Orange.png
     Yellow.png
     Purple.png
     Black.png

And my code which tries to get this list of files (the color pngs) is

NSError *error = nil;
NSString *pathString = [[NSString alloc] init];
pathString = [[NSString alloc] initWithString:@"/Cards/Colors/"];
NSArray *fileList = [[NSArray alloc] init];
fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathString error: &error];
[pathString release];
NSLog(@"%@", error);
// this is always 0
NSLog(@"file list has %i items", [fileList count]);

The NSError I get is

Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x596db00 {NSUserStringVariant=(
    Folder
), NSFilePath=/Cards/Color/, NSUnderlyingError=0x5925ef0 "The operation couldn’t be completed. No such file or directory"}

Any ideads where I am going wrong?

解决方案

You're initializing pathString to the absolute path /Cards/Colors/. This path is a system-wide path, so on the iPhone, far outside your app's sandbox.

Try this instead:

NSString *pathString = [[NSBundle mainBundle] pathForResource:@"Cards/Colors" ofType:nil];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathString error: &error];

(Note that the way you have your code in the question, you alloc/init fileList, then immediately leak the object by assigning to it the results of contentsOfDirectoryAtPath:error:. This is a bug.)

这篇关于来自文件夹内文件名的 NSString 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆