ios – 以编程方式创建目录

前端之家收集整理的这篇文章主要介绍了ios – 以编程方式创建目录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在applicationSupportDirectory中创建一个目录.我的理解是applicationSupportDirectory不允许用户查看其中的数据.这就是我选择它的原因.但是,我在下面使用的代码似乎失败了,我不知道为什么.

谁能告诉我我做错了什么?谢谢!

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,NSUserDomainMask,YES);
NSString *applicationDirectory = [paths objectAtIndex:0]; // Get directory
NSString *dataPath = [applicationDirectory stringByAppendingPathComponent:@"drawings"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){

NSError* error;
if([[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error])
  ;// success
else
{
  NSLog(@"Failed");
}
}
}

解决方法

NSString *directoryName = @"drawing";

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,YES);

NSString *applicationDirectory = [paths objectAtIndex:0];
NSString *filePathAndDirectory = [applicationDirectory stringByAppendingPathComponent:directoryName];
NSError *error;

if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
                               withIntermediateDirectories:YES
                                                attributes:nil
                                                     error:&error])
{
    NSLog(@"Create directory error: %@",error);
}

希望它会对你有所帮助

原文链接:https://www.f2er.com/iOS/328655.html

猜你在找的iOS相关文章