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 に変更しようとしましたが、役に立ちませんでした