1

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

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

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

我的代码试图获取这个文件列表(颜色 pngs)是

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

任何想法我哪里出错了?

4

1 回答 1

6

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

试试这个:

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

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

于 2010-08-03T19:50:58.260 回答