2009-07-14 4 views
0

iPhone 장치에서 NSFileManager로 디렉토리 및 파일을 만드는 데 문제가 있습니다. 아래에 표시된 내 코드는 시뮬레이터에서 제대로 작동하지만 기기에서는 작동하지 않습니다. 제발 도와주세요. 이리 문제가 될 수있는 몇 가지 방향으로, 모든 답장을 보내 주셔서 감사합니다 ..NSFileManager는 시뮬레이터에서 정상적으로 작동하지만 장치에서는 작동하지 않습니다.

은 내가 먼저 디렉토리 이런 식으로 만드는거야 :

NSFileManager *fileMgr = [NSFileManager defaultManager]; 
NSArray *arPaths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES); 
NSString *appPath = [[NSString alloc] init]; 
appPath = [arPaths objectAtIndex:0]; 

strDownloadsDir = [[NSString alloc] init]; 
strDownloadsDir = [[appPath stringByAppendingPathComponent:@"/Other"] copy]; 
if(![fileMgr fileExistsAtPath:strDownloadsDir]) 
    [fileMgr createDirectoryAtPath:strDownloadsDir attributes:nil]; 

을 한 후 나는이 디렉토리에이 방법을 새 파일을 만들려고 해요 :

NSString *filePath = [strDownloadsDir stringByAppendingPathComponent:strDlFileName]; 
//Test whether this file exists, if not, create it 
NSLog(@"%@", filePath); 
if(![fileMgr fileExistsAtPath:filePath]) 
{ 
    if([fileMgr createFileAtPath:filePath contents:nil attributes:nil]) 
     NSLog(@"Creating new file at path %@", filePath); 
    else 
     NSLog(@"Failed to create new file."); 
} 

은 전체 NSFileManager를에 문제가 있음을 보인다, 나는이

NSArray *arPaths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES); 
NSString *appPath = [[NSString alloc] init]; 
appPath = [arPaths objectAtIndex:0]; 
에 의해 주어진 경로로 fileExistAtPath을 사용하고 때 때문에 6,

는 너무 작동하지 않습니다, 나는 NSDocumentsDirectory로 디렉토리를 변경하려고하지만 방법을 사용하기 때문에

+0

많은 NSString이 누출됩니다. 불필요한'[[NSString alloc] init];'을하지 마십시오. – kennytm

답변

1

가있을 수 있습니다 도움이되지 않았다 "createDirectoryAtPath : attrubutes는"..... 사과로 사용되지 않습니다

0

응용 프로그램의 sandbox 디렉토리 아래에 파일을 만들 수 있습니다. Apple은 귀하가 파일을 생성하는 것을 허용하지 않습니다.

휴대 전화에서 실패한 이유가 무엇인가요?

0
[[NSFileManager defaultManager] createDirectoryAtPath:strPath withIntermediateDirectories:YES attributes:nil error:nil]; 

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *strFile = [documentsDirectory stringByAppendingPathComponent:@"hello/config.plist"]; 
     NSLog(@"strFile: %@", strFile); 

     NSString *strPath = [documentsDirectory stringByAppendingPathComponent:@"hello"]; 
     if (![[NSFileManager defaultManager] fileExistsAtPath:strPath]) { 
      NSLog(@"there is no Directory: %@",strPath); 
      [[NSFileManager defaultManager] createDirectoryAtPath:strPath withIntermediateDirectories:YES attributes:nil error:nil]; 
     } 

     NSArray *keys = [NSArray arrayWithObjects: @"username", @"password", @"serverName", @"serverPort", @"autoSave", nil]; 
     NSArray *values = [NSArray arrayWithObjects: @"11", @"11", @"11", @"11", @"11", nil]; 

     NSDictionary *configInfo = [[NSDictionary alloc] initWithObjects: values forKeys:keys]; 
     if (![configInfo writeToFile: strFile atomically: YES]) { 
      NSLog(@"write file error"); 
     } 
관련 문제