特定の開始パスからファイルシステムの構造をモデル化しようとしています。目標はNSOutlineView
、そのパス以降のファイルシステムの標準を作成することです。
というモデル オブジェクトがありますfileSystemItem
。次の (非常に標準的な) 関係とプロパティがあります。
parentItem
fileSystemItem
(別のオブジェクト を指します)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
ますか? つまり、おそらくそれがクラスが存在する理由ですよね?