1

プロジェクトにドラッグしたフォルダーの内容の NSStrings の配列を作成しようとしていますが、後で配列内の項目を数えると、常に 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"}

私が間違っているアイデアはありますか?

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];

fileList(質問にコードを含める方法では、alloc / initを実行し、その結果をオブジェクトに割り当てることで、すぐにオブジェクトをリークすることに注意してくださいcontentsOfDirectoryAtPath:error:。これはバグです。)

于 2010-08-03T19:50:58.260 に答える