2009-07-14 23 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."); 
} 

私はこの

NSArray *arPaths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES); 
NSString *appPath = [[NSString alloc] init]; 
appPath = [arPaths objectAtIndex:0]; 
で与えられたパスでfileExistAtPathを使用しているときので、全体NSFileManagerに何か問題がありますようです

それはあまりにも動作していない、私はNSDocumentsDirectoryにディレクトリを変更しようとしたが、それは

+0

多くのNSStringが漏洩しています。不要な '[[NSString alloc] init];'を実行しないでください。 – kennytm

答えて

1

は方法の使用しているためかもしれ助けにはならなかった「createDirectoryAtPath:attrubutes:」りんごによって非難され.....

0

アプリケーションのサンドボックスディレクトリの下にのみファイルを作成できます。 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"); 
     } 
関連する問題