2011-12-08 5 views
8
내 자원 폴더에서 폴더 구조를하고있는 중이 야

같은 ... 자원 =>의 MyData => 다음 S1 => Name.png에, data.pptiPhone의 리소스 폴더에서 폴더 및 파일 목록을 가져 오는 방법은 무엇입니까?

는 이제 모든 폴더 목록 및 파일 이름을 얻으려면 S1 . 여기서 MyData 이름은 정적 인 다른 것일 수 있습니다. S1, S2, S3 및 파일 수를 추가 할 수 있습니다. Objective C를 통해 이러한 내용을 읽는 방법은 무엇입니까? 나는 아래 코드를 시도했다. 당신은이 같은 Resources 디렉토리의 경로를 얻을 수 있습니다

답변

12

NSString *bundlePathName = [[NSBundle mainBundle] bundlePath]; 
    NSString *dataPathName = [bundlePathName stringByAppendingPathComponent:@"Resources"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:dataPathName]) { 
     NSLog(@"%@ exists", dataPathName); 
     BOOL isDir = NO; 
     [fileManager fileExistsAtPath:dataPathName isDirectory:(&isDir)]; 
     if (isDir == YES) { 
      NSLog(@"%@ is a directory", dataPathName); 
      NSArray *contents; 
      contents = [fileManager contentsOfDirectoryAtPath:dataPathName error:nil]; 
      for (NSString *entity in contents) { 
       NSLog(@"%@ is within", entity); 
      } 
     } else { 
      NSLog(@"%@ is not a directory", dataPathName); 
     } 
    } else { 
     NSLog(@"%@ does not exist", dataPathName); 
    } 

감사합니다,

NSString * resourcePath = [[NSBundle mainBundle] resourcePath]; 

그런 다음 경로로 Documents을 추가, 그리고
NSString * documentsPath = [resourcePath stringByAppendingPathComponent:@"Documents"]; 

당신이 할 수있는의 디렉토리 목록 API 중 하나를 사용하십시오..

NSError * error; 
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error]; 
관련 문제