2011-12-08 17 views
8

sto facendo struttura di cartelle nella mia cartella risorsa come risorsa ... => MyData => S1 poi nella S1 => Name.png, data.pptCome ottenere l'elenco di cartelle e file dalla cartella delle risorse in iPhone?

Ora voglio ottenere tutti elenco delle cartelle e nomi di file . Qui il nome MyData sarà solo statico, altri potrebbero cambiare.Come posso aggiungere S1, S2, S3 e il numero di file in quello. Quindi come leggere questi contenuti attraverso l'obiettivo C? Ho provato sotto il codice.

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); 
    } 

Grazie,

risposta

12

È possibile ottenere il percorso della directory Resources come questo,

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

quindi aggiungere il Documents al percorso,

NSString * documentsPath = [resourcePath stringByAppendingPathComponent:@"Documents"]; 

Poi si può utilizzare una qualsiasi delle directory che elenca le API di NSFileManager.

NSError * error; 
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error]; 
Problemi correlati