0

iPhone デバイスで 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.");
}

これで指定されたパスでfileExistAtPathを使用しているとき、NSFileManager全体に何か問題があるようです

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

それも機能していません。ディレクトリを NSDocumentsDirectory に変更しようとしましたが、役に立ちませんでした

4

3 に答える 3

1

メソッド「createDirectoryAtPath:attrubets:」の使用がアップルによって非推奨になっていることが原因である可能性があります。

于 2010-01-11T10:28:23.917 に答える
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");
        }
于 2011-04-15T03:19:11.997 に答える
0

アプリケーションのサンドボックス ディレクトリの下にのみファイルを作成できます。Apple は、そこからファイルを作成することを許可しません。

それがあなたの電話で失敗した理由ですか?

于 2010-06-29T09:11:58.923 に答える