2009-09-28 4 views
0

방금 ​​Objective-C/Cocoa를 배우면서 발견 된 파일의 크기를 표시하기 위해 코드를 확장 할 수있는 방법이 조금 확실하지 않습니다.FileSize를 확장하도록 확장 하시겠습니까?

EDIT 2

나는 코멘트를 환영 ... 아래 SMorgan & 피터의 의견을 촬영하고 코드로 근무했다.

// ------------------------------------------------------------------- ** 
// DISC: FILEWALKER ..... (cocoa_fileWalker.m) 
// DATE: 28th September 2009 
// DESC: List all "*.exr" files in specified directory 
// ------------------------------------------------------------------- ** 

#import <Foundation/Foundation.h> 

int main (int argc, const char * argv[]) { 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    NSString *fileName; 
    NSDictionary *attrDir; 
    NSError *myError; 
    NSNumber *fileSize; 
    NSFileManager *manager = [NSFileManager defaultManager]; 
    NSString *rootPath = [@"~/Pictures" stringByExpandingTildeInPath]; 
    NSMutableArray *fileArray = [[NSMutableArray alloc] init]; 
    NSMutableString *newPath = [[NSMutableString alloc] init]; 

    NSLog(@"Home: %@",rootPath); 

    for(fileName in [manager enumeratorAtPath:rootPath]){ 
     if ([[fileName pathExtension] isEqual:@"exr"]) { 
      [fileArray addObject:fileName]; 

      [newPath setString:rootPath]; 
      [newPath appendString:@"/"]; 
      [newPath appendString:fileName]; 
      attrDir = [manager attributesOfItemAtPath:newPath error:&myError]; 
      fileSize = [attrDir objectForKey: @"NSFileSize"]; 
      NSLog(@"File: /%@ Size: %@", fileName, fileSize); 
     } 
    } 

    [fileArray release]; 
    [newPath release]; 
    [pool drain]; 
    return 0; 
} 
// ------------------------------------------------------------------- ** 

환호 게리

답변

2

당신은 반환 된 사전에서 NSFileSize를 읽을 다음 -[NSFileManager fileAttributesAtPath:traverseLink:]를 사용 (또는 당신은 단지 10.5, -[NSFileManager attributesOfItemAtPath:error:]을 대상으로하는 경우), 그리고 것입니다.

+0

초보자 용으로 어떻게 작성했는지 모르겠다. (파일 이름 ... 루프, 내 기존 코드에 어떻게 적용 할 수 있습니까?) – fuzzygoat

+1

예, 사실은 그렇습니다. 'enumeratorAtPath :'에 의해 생성 된 경로 이름은 여러분이 열거하고있는 디렉토리에 대한 상대 경로로 절대 경로 이름이 아니므로 배열로 수집하여 나중에 문맥 밖으로 사용할 수 없습니다 .' stringByAppendingPathComponent :'를 사용하여 리프 경로를 디렉토리 경로에 통합하십시오. –

관련 문제