2013-06-10 3 views
0

지금 ios에서 파일 및 폴더를 만들고 관리하는 방법을 배우고 있습니다. 하지만 폴더 부분을 제대로 처리하지 못하는 것 같습니다. 아래 코드에 어떤 문제가 있습니까?IOS - 파일 및 폴더 관리

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSFileManager *filemgr; 
    NSString *dataFile; 
    NSString *docsDir; 
    NSString *newDir; 
    NSArray *dirPaths; 
    BOOL isDir; 

filemgr = [NSFileManager defaultManager]; 

// Identify the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains(
               NSDocumentDirectory, NSUserDomainMask, YES); 

docsDir = dirPaths[0]; 

// Build the path to the data file 
if ([filemgr fileExistsAtPath:@"newfolder" isDirectory:&isDir] && isDir) { 
    NSLog(@"folder already created!"); 
}else{ 
    NSLog(@"create new folder now..."); 
    newDir = [docsDir stringByAppendingPathComponent:@"newfolder"]; 
} 

dataFile = [docsDir stringByAppendingPathComponent: 
      @"/newfolder/datafile.dat"]; 

// Check if the file already exists 
if ([filemgr fileExistsAtPath: dataFile]) 
{ 
    // Read file contents and display in textBox 
    NSData *databuffer; 
    databuffer = [filemgr contentsAtPath: dataFile]; 

    NSString *datastring = [[NSString alloc] 
          initWithData: databuffer 
          encoding:NSASCIIStringEncoding]; 

    _textBox.text = datastring; 
} 

} 


- (IBAction)saveText:(id)sender { 
    NSFileManager *filemgr; 
    NSData *databuffer; 
    NSString *dataFile; 
    NSString *docsDir; 
    NSArray *dirPaths; 

    filemgr = [NSFileManager defaultManager]; 

    dirPaths = NSSearchPathForDirectoriesInDomains(
                NSDocumentDirectory, NSUserDomainMask, YES); 

    docsDir = dirPaths[0]; 
    dataFile = [docsDir 
       stringByAppendingPathComponent: @"/newfolder/datafile.dat"]; 
    databuffer = [_textBox.text 
        dataUsingEncoding: NSASCIIStringEncoding]; 
    [filemgr createFileAtPath: dataFile 
        contents: databuffer attributes:nil]; 
} 

나는 일이 있지만, 매번 내가 그것을 나에게 바로 그겁니다 이미 전에 텍스트에 넣어 빈 텍스트 필드를주는 시뮬레이션을 실행 무엇인지 모르겠습니다. 난 Here에서 자습서를 따라 Google 디렉토리 생성 부분에 대한 다른 예제. 뭐가 잘못 됐어 ?? 감사!

답변

2

문제는이 라인에 있습니다

if ([filemgr fileExistsAtPath:@"newfolder" isDirectory:&isDir] && isDir) { 
    NSLog(@"folder already created!"); 
당신은 fileExistsAtPath 등의 인수로 전체 경로를 통과해야

: 경우에, 여기

NSSTring *folderPath = [docsDir stringByAppendingPathComponent: 
      @"newfolder"]; 
if ([filemgr fileExistsAtPath:folderPath isDirectory:&isDir] && isDir) { 
     NSLog(@"folder already created!"); 
0

버전을 작업 업데이트 누구나 찾고 있습니다 :

newDir = [docsDir stringByAppendingPathComponent:@"newfolder"]; 
    if ([filemgr fileExistsAtPath:newDir isDirectory:&isDir] && isDir) { 
     NSLog(@"folder already created!"); 
    }else{ 
     NSLog(@"create new folder now..."); 
     [filemgr createDirectoryAtPath:newDir withIntermediateDirectories:YES 
          attributes:nil error: NULL]; 

    } 
+0

이 질문은 추가 답변이 아닌 질문의 일부 여야합니다. 모든 대답은 다른 대답에 포함 된 정보를 삽입하는 것입니다. – borrrden