iOS中在沙盒中创建指定文件夹
2018-02-06 本文已影响354人
青梅煮码
//获取Document文件
NSString * docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * rarFilePath = [docsdir stringByAppendingPathComponent:@"Announcement"];//将需要创建的串拼接到后面
NSString * dataFilePath = [docsdir stringByAppendingPathComponent:@"AnnouncementData"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
BOOL dataIsDir = NO;
// fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
BOOL existed = [fileManager fileExistsAtPath:rarFilePath isDirectory:&isDir];
BOOL dataExisted = [fileManager fileExistsAtPath:dataFilePath isDirectory:&dataIsDir];
if ( !(isDir == YES && existed == YES) ) {//如果文件夹不存在
[fileManager createDirectoryAtPath:rarFilePath withIntermediateDirectories:YES attributes:nil error:nil];
}
if (!(dataIsDir == YES && dataExisted == YES) ) {
[fileManager createDirectoryAtPath:dataFilePath withIntermediateDirectories:YES attributes:nil error:nil];
}