NSFileManager 在模拟器上工作正常,但在设备上不行
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能是因为苹果不推荐使用方法“createDirectoryAtPath: attrubutes:”......
may be because the use of method " createDirectoryAtPath: attrubutes: " is DEPRECATED by apple.....
您只能在应用程序的沙箱目录下创建文件。 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 ?