我有在 NSHomeDirectory 下创建文件夹的代码,但它总是失败,我不明白为什么。它应该可以工作,但它一直说不允许该操作。有任何想法吗?
NSURL *homeURL = [NSURL fileURLWithPath:NSHomeDirectory() isDirectory:YES];
NSURL *previewsURL = [homeURL URLByAppendingPathComponent:@"Previews"];
DebugLog(@"homeURL: %@", homeURL);
DebugLog(@"previewsURL: %@", previewsURL);
BOOL isDirectory = TRUE;
if (![[NSFileManager defaultManager] fileExistsAtPath:previewsURL.path isDirectory:&isDirectory]) {
DebugLog(@"isDirectory: %@", isDirectory ? @"YES" : @"NO");
NSAssert(isDirectory, @"Must be a directory");
NSError *error = nil;
if (![[NSFileManager defaultManager] createDirectoryAtPath:previewsURL.path withIntermediateDirectories:TRUE attributes:nil error:&error]) {
DebugLog(@"Error: %@", error); // WHY?!
}
else {
BOOL isDirectory = TRUE;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:previewsURL.path isDirectory:&isDirectory];
NSAssert(exists, @"Directory must exist");
}
}