无法在iOS 7中创建新目录

时间:2014-01-31 09:37:18

标签: ios ios7 nsfilemanager

我无法在应用程序的文件夹(iOS 7)中创建新目录,我使用此代码...

    NSFileManager *filemgr;
    NSArray *dirPaths;
    NSString *docsDir;
    NSString *newDir;

    filemgr =[NSFileManager defaultManager];

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                   NSUserDomainMask, YES);

    docsDir = dirPaths[0];
    newDir = [docsDir stringByAppendingPathComponent:@"myPictures"];

在检查日志目录时,它显示了具有新目录的路径名,但是当在finder中检查时,文档目录仍为空。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

试试这个,

NSFileManager *filemgr;
NSArray *dirPaths;
NSString *docsDir;
NSString *newDir;

filemgr =[NSFileManager defaultManager];

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                               NSUserDomainMask, YES);

docsDir = dirPaths[0];
newDir = [docsDir stringByAppendingPathComponent:@"/myPictures"];
if(![filemgr fileExistsAtPath:newDir])
{
    NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/myPictures"]];
    NSError *error;
    [[NSFileManager defaultManager] createDirectoryAtPath:[docsDir stringByAppendingPathComponent:@"/myPictures"] withIntermediateDirectories:YES attributes:nil error:&error];
    [data writeToFile:newDir atomically:YES];
}