文件夹内文件名的 NSString 数组?

发布于 2024-09-13 02:27:01 字数 1029 浏览 6 评论 0原文

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

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

-Cards
  -Colors
     Blue.png
     Green.png
     Orange.png
     Yellow.png
     Purple.png
     Black.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: &error];
[pathString release];
NSLog(@"%@", error);
// this is always 0
NSLog(@"file list has %i items", [fileList count]);

我得到的 NSError 是

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"}

任何我出错的地方吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘然心甜 2024-09-20 02:27:01

您将 pathString 初始化为绝对路径 /Cards/Colors/。该路径是系统范围的路径,因此在 iPhone 上,远离应用程序的沙箱。

请尝试这样做:(

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

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

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文