1

特定の開始パスからファイルシステムの構造をモデル化しようとしています。目標はNSOutlineView、そのパス以降のファイルシステムの標準を作成することです。

というモデル オブジェクトがありますfileSystemItem。次の (非常に標準的な) 関係とプロパティがあります。

  • parentItemfileSystemItem(別のオブジェクト を指します)
  • isLeaf (YESファイルの場合NO、フォルダーの場合)
  • childrenItems(その他の配列fileSystemItems)
  • fullPath( NSString; オブジェクトのファイルパス)

私の質問はNSDirectoryEnumerator、モデルを構築するためにどのように使用するのですか? 私がこれを行う場合:

// NOTE: can't do "while (file = [dirEnum nextObject]) {...} because that sets 
// file to an auto-released string that doesn't get released until after ALL 
// iterations of the loop are complete. For large directories, that means our 
// memory use spikes to hundreds of MBs. So we do this instead to ensure that 
// the "file" string is released at the end of each iteration and our overall 
// memory footprint stays low.

NSDirectoryEnumerator *dirEnum = [aFileManager enumeratorAtPath:someStartingPath];
BOOL keepRunning = YES;
while (keepRunning)
{
    NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc] init];

    NSString *file = [dirEnum nextObject];
    if (file == nil) break;

    // ... examine "file". Create a fileSystemItem object to represent this item.
    // If it's a folder, we need to create a fileSystemItem for each item in the folder
    // and each fileSystemItem's "parentItem" relationship needs to be set to the 
    // fileSystemItem we're creating right here for "file." How can I do this inside
    // the directoryEnumerator, because as soon as we go to the next iteration of the    
    // loop (to handle the first item in "file" if "file" is a folder), we lose the  
    // reference to the fileSystemItem we created in THIS iteration of the loop for 
    // "file". Hopefully that makes sense... 

    [innerPool drain];
}

各アイテムを参照する再帰関数を記述し、startingPathそのアイテムがフォルダーの場合は、そのフォルダーで自分自身を再度呼び出すなどして、モデルを構築する方法を確認できます。しかし、どうすればモデルを構築できNSDirectoryEnumeratorますか? つまり、おそらくそれがクラスが存在する理由ですよね?

4

2 に答える 2

0

別の種類のディレクトリ列挙を使用することができます。

enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:

この列挙子には追加の便利なオプションがありNSURLNameKeyNSURLIsDirectoryKey、 、 などのプリフェッチされた属性を使用して NSURL インスタンスを繰り返すNSURLParentDirectoryURLKeyことができます。再帰の使用を取り除くのに役立ちます。

于 2013-11-14T21:10:32.643 に答える
-2

そのファイルがディレクトリの場合は、新しいディレクトリ列挙子を作成する必要があります。NSDirectoryEnumerator は、システム上のすべてのディレクトリではなく、1 つのディレクトリを列挙します。はい、再帰を使用する必要があります。

于 2012-01-08T10:39:25.197 に答える