I've found this way of creating a directory if it does not exist. But it looks a bit wonky and I am afraid that this can go wrong in 1 of 1000 attempts.
如果目录不存在,我发现了这种创建目录的方法。但它看起来有点不稳定,我担心这会在1000次尝试中出现错误。
if(![[NSFileManager defaultManager] fileExistsAtPath:bundlePath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:bundlePath withIntermediateDirectories:YES attributes:nil error:NULL];
}
There is only this awkward method fileExistsAtPath which also looks for files and not only directories. But for me, the dangerous thing is: What if this goes wrong? What shall I do? What is best practice to guarantee that the directory is created, and only created when it does not exist?
只有这个笨拙的方法fileExistsAtPath也可以查找文件,而不仅仅是目录。但对我来说,危险的是:如果这出错怎么办?我该怎么办?保证创建目录的最佳做法是什么,只有在目录不存在时才创建?
I know file system operations are never safe. Device could loose battery power suddenly just in the moment where it began shoveling the bits from A to B. Or it can stumble upon a bad bit and hang for a second. Maybe in some seldom cases it returns YES even if there is no directory. Simply put: I don't trust file system operations.
我知道文件系统操作永远不会安全。设备可能会在它开始从A到B的位置时突然耗尽电池电量。或者它会偶然发现一个坏位并挂了一秒钟。也许在一些很少的情况下,即使没有目录,它也会返回YES。简单地说:我不相信文件系统操作。
How can I make this absolutely safe?
我怎样才能绝对安全?
4 个解决方案
#1
67
You can actually skip the if
, even though Apple's docs say that the directory must not exist, that is only true if you are passing withIntermediateDirectories:NO
你实际上可以跳过if,即使Apple的文档说该目录不能存在,只有在你传递中间目录时才会这样:
That puts it down to one call. The next step is to capture any errors:
这归结为一个电话。下一步是捕获任何错误:
NSError * error = nil;
[[NSFileManager defaultManager] createDirectoryAtPath:bundlePath
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (error != nil) {
NSLog(@"error creating directory: %@", error);
//..
}
This will not result in an error if the directory already exists.
如果目录已存在,则不会导致错误。
#2
5
For Swift 3.0
对于Swift 3.0
do {
try FileManager.default.createDirectory(atPath: folder, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
#3
3
NSFileManager *fileManager= [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:directory isDirectory:&isDir])
if(![fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:NULL])
NSLog(@"Error: Create folder failed %@", directory);
From an SO topic here.
来自SO主题。
After creating a directory, you can flush the file system then check to see if your newly created directory exists. This is probably overkill, but you can never have too much overkill.
创建目录后,可以刷新文件系统,然后检查新创建的目录是否存在。这可能是矫枉过正,但你永远不会有太多的矫枉过正。
#4
1
In swift 2 it looks like this:
在swift 2中它看起来像这样:
do {
try NSFileManager.defaultManager().createDirectoryAtPath(pathURL.absoluteString, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
#1
67
You can actually skip the if
, even though Apple's docs say that the directory must not exist, that is only true if you are passing withIntermediateDirectories:NO
你实际上可以跳过if,即使Apple的文档说该目录不能存在,只有在你传递中间目录时才会这样:
That puts it down to one call. The next step is to capture any errors:
这归结为一个电话。下一步是捕获任何错误:
NSError * error = nil;
[[NSFileManager defaultManager] createDirectoryAtPath:bundlePath
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (error != nil) {
NSLog(@"error creating directory: %@", error);
//..
}
This will not result in an error if the directory already exists.
如果目录已存在,则不会导致错误。
#2
5
For Swift 3.0
对于Swift 3.0
do {
try FileManager.default.createDirectory(atPath: folder, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
#3
3
NSFileManager *fileManager= [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:directory isDirectory:&isDir])
if(![fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:NULL])
NSLog(@"Error: Create folder failed %@", directory);
From an SO topic here.
来自SO主题。
After creating a directory, you can flush the file system then check to see if your newly created directory exists. This is probably overkill, but you can never have too much overkill.
创建目录后,可以刷新文件系统,然后检查新创建的目录是否存在。这可能是矫枉过正,但你永远不会有太多的矫枉过正。
#4
1
In swift 2 it looks like this:
在swift 2中它看起来像这样:
do {
try NSFileManager.defaultManager().createDirectoryAtPath(pathURL.absoluteString, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}