我想在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); }
希望它会对你有所帮助