2011-09-05 3 views
1

주어진 시작 경로에서 파일 시스템의 구조를 모델링하려고합니다. 목표는 해당 경로에서 파일 시스템의 표준 NSOutlineView을 작성하는 것입니다.NSDirectoryEnumerator를 사용하여 파일 시스템 모델하기

fileSystemItem이라는 모델 개체가 있습니다. ;

  • childrenItems (다른 fileSystemItems의 배열)
  • (다른 fileSystemItem 객체 점)

    • parentItem
    • isLeaf (폴더 NO 파일 YES) : 그것은 다음 (매우 표준) 관계와 속성이 있습니다
    • 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으로 모델을 만들려면 어떻게해야합니까? 아마 그게 수업이 존재하는 이유 일거야, 그렇지?

    답변

    -2

    해당 파일이 디렉터리 인 경우 새 디렉터리 열거자를 만들어야합니다. NSDirectoryEnumerator는 시스템의 모든 디렉토리가 아니라 하나의 디렉토리를 나열합니다. 그렇습니다. 재귀를 사용해야합니다.

    enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:

    이 열거 유용한 추가 옵션을 가지고 있으며, 프리 페치 된 속성 NSURL 인스턴스를 반복 할 수와 같은 NSURLNameKey, NSURLIsDirectoryKey, NSURLParentDirectoryURLKey 등 :

    0

    이 디렉토리 열거 다른 종류를 사용할 수있다 .. 재귀 사용을 제거하는 데 도움이 될 수 있습니다.