文件夹内文件名的 NSString 数组?
我正在尝试创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将
pathString
初始化为绝对路径/Cards/Colors/
。该路径是系统范围的路径,因此在 iPhone 上,远离应用程序的沙箱。请尝试这样做:(
请注意,您在问题中使用代码的方式是分配/初始化
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:
(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 ofcontentsOfDirectoryAtPath:error:
. This is a bug.)