NSFileManager 在模拟器上工作正常,但在设备上不行

发布于 2024-07-27 04:16:04 字数 1415 浏览 16 评论 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.");
}

似乎整个 NSFileManager 有问题,因为当我使用 fileExistAtPath 和由此给出的路径时,

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

它也不起作用,我尝试将目录更改为 NSDocumentsDirectory 但它没有帮助

I have problem creating directories and files with NSFileManager on the iPhone device.
My code, shown below, works fine on the simulator, but not on the device, could you please help me? Gimme some directions where the problem may be, thanks for every reply..

I'm first creating directories this way:

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];

and then I'm trying to create new file in this directory this way:

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.");
}

It seems that there's something wrong with whole NSFileManager, because when I'm using fileExistAtPath with a path given by this

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

it is not working too, I tried to change directory to NSDocumentsDirectory but it did not help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤芳又自赏 2024-08-03 04:16:04

可能是因为苹果不推荐使用方法“createDirectoryAtPath: attrubutes:”......

may be because the use of method " createDirectoryAtPath: attrubutes: " is DEPRECATED by apple.....

給妳壹絲溫柔 2024-08-03 04:16:04

您只能在应用程序的沙箱目录下创建文件。 Apple 不会允许您从中创建文件。

这就是你手机失败的原因吗?

You only can create file under the sandbox directory of your application. Apple will not permit you create file out of that.

Is that the reason you failed on your phone ?

自找没趣 2024-08-03 04:16:04
[[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");
        }
[[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");
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文