Is there a safer way to create a directory if it does not exist?
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.
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?
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.
How can I make this absolutely safe?
ios objective-c filesystems nsfilemanager nsdocumentdirectory
add a comment |
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.
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?
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.
How can I make this absolutely safe?
ios objective-c filesystems nsfilemanager nsdocumentdirectory
add a comment |
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.
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?
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.
How can I make this absolutely safe?
ios objective-c filesystems nsfilemanager nsdocumentdirectory
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.
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?
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.
How can I make this absolutely safe?
ios objective-c filesystems nsfilemanager nsdocumentdirectory
ios objective-c filesystems nsfilemanager nsdocumentdirectory
edited Jan 7 '16 at 9:30
Meet Doshi
2,68282866
2,68282866
asked May 25 '11 at 14:13
dontWatchMyProfiledontWatchMyProfile
19.1k47159251
19.1k47159251
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
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
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.
Great! What should I do if there occurs an error? Does it make sense to attempt and try again? (at least a few times...? and what if that does not help?)
– dontWatchMyProfile
May 25 '11 at 15:00
I'm not sure what to do if there is an error. It depends how fancy you want to get. You could look for specific errors and handle them differently, or try to create a different path. Perhaps let the user choose a different one?
– e.James
May 25 '11 at 15:17
5
You need to check the return value, not the error pointer
– Sven
Feb 9 '13 at 8:17
1
@Sven: I don't see how it makes a difference to the logic?
– e.James
Feb 10 '13 at 14:53
Cocoa error handling generally does not promise that the error pointer will beNULL
on success, only that it will be populated on failure.
– Josh Caswell
Oct 21 '17 at 19:56
add a comment |
For Swift 3.0
do {
try FileManager.default.createDirectory(atPath: folder, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
add a comment |
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.
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.
What do you mean by "flush the file system"?
– dontWatchMyProfile
May 25 '11 at 15:02
dontWatchMyProfile: Writes to a disk (or in this case, FLASH storage) are often first written to much faster RAM, then committed to main storage as performance and load allow. Flushing that cache, or "syncing", commands the system to "write the entire contents of the cache right now." This is important in case, as the OP says, power is lost. I have little information regarding file IO on the iPhone and related devices, but a hopefully relevant SO thread exists here: stackoverflow.com/questions/459537/…
– Charles Burns
May 25 '11 at 16:58
2
Between line two and line three you should add a comment like this: // Right here, we switch processes and someone else can create a file with the name we just passed to fileExistsAtPath:isDirectory: - so this check isn't really worth doing.
– James Moore
Nov 29 '12 at 18:48
add a comment |
In swift 2 it looks like this:
do {
try NSFileManager.defaultManager().createDirectoryAtPath(pathURL.absoluteString, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
add a comment |
Swift 4.2
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let imagesPath = documentsURL.appendingPathComponent("Images")
do
{
try FileManager.default.createDirectory(atPath: imagesPath.path, withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError
{
NSLog("Unable to create directory (error.debugDescription)")
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6125819%2fis-there-a-safer-way-to-create-a-directory-if-it-does-not-exist%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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.
Great! What should I do if there occurs an error? Does it make sense to attempt and try again? (at least a few times...? and what if that does not help?)
– dontWatchMyProfile
May 25 '11 at 15:00
I'm not sure what to do if there is an error. It depends how fancy you want to get. You could look for specific errors and handle them differently, or try to create a different path. Perhaps let the user choose a different one?
– e.James
May 25 '11 at 15:17
5
You need to check the return value, not the error pointer
– Sven
Feb 9 '13 at 8:17
1
@Sven: I don't see how it makes a difference to the logic?
– e.James
Feb 10 '13 at 14:53
Cocoa error handling generally does not promise that the error pointer will beNULL
on success, only that it will be populated on failure.
– Josh Caswell
Oct 21 '17 at 19:56
add a comment |
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
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.
Great! What should I do if there occurs an error? Does it make sense to attempt and try again? (at least a few times...? and what if that does not help?)
– dontWatchMyProfile
May 25 '11 at 15:00
I'm not sure what to do if there is an error. It depends how fancy you want to get. You could look for specific errors and handle them differently, or try to create a different path. Perhaps let the user choose a different one?
– e.James
May 25 '11 at 15:17
5
You need to check the return value, not the error pointer
– Sven
Feb 9 '13 at 8:17
1
@Sven: I don't see how it makes a difference to the logic?
– e.James
Feb 10 '13 at 14:53
Cocoa error handling generally does not promise that the error pointer will beNULL
on success, only that it will be populated on failure.
– Josh Caswell
Oct 21 '17 at 19:56
add a comment |
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
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.
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
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.
answered May 25 '11 at 14:32
e.Jamese.James
81.6k31159203
81.6k31159203
Great! What should I do if there occurs an error? Does it make sense to attempt and try again? (at least a few times...? and what if that does not help?)
– dontWatchMyProfile
May 25 '11 at 15:00
I'm not sure what to do if there is an error. It depends how fancy you want to get. You could look for specific errors and handle them differently, or try to create a different path. Perhaps let the user choose a different one?
– e.James
May 25 '11 at 15:17
5
You need to check the return value, not the error pointer
– Sven
Feb 9 '13 at 8:17
1
@Sven: I don't see how it makes a difference to the logic?
– e.James
Feb 10 '13 at 14:53
Cocoa error handling generally does not promise that the error pointer will beNULL
on success, only that it will be populated on failure.
– Josh Caswell
Oct 21 '17 at 19:56
add a comment |
Great! What should I do if there occurs an error? Does it make sense to attempt and try again? (at least a few times...? and what if that does not help?)
– dontWatchMyProfile
May 25 '11 at 15:00
I'm not sure what to do if there is an error. It depends how fancy you want to get. You could look for specific errors and handle them differently, or try to create a different path. Perhaps let the user choose a different one?
– e.James
May 25 '11 at 15:17
5
You need to check the return value, not the error pointer
– Sven
Feb 9 '13 at 8:17
1
@Sven: I don't see how it makes a difference to the logic?
– e.James
Feb 10 '13 at 14:53
Cocoa error handling generally does not promise that the error pointer will beNULL
on success, only that it will be populated on failure.
– Josh Caswell
Oct 21 '17 at 19:56
Great! What should I do if there occurs an error? Does it make sense to attempt and try again? (at least a few times...? and what if that does not help?)
– dontWatchMyProfile
May 25 '11 at 15:00
Great! What should I do if there occurs an error? Does it make sense to attempt and try again? (at least a few times...? and what if that does not help?)
– dontWatchMyProfile
May 25 '11 at 15:00
I'm not sure what to do if there is an error. It depends how fancy you want to get. You could look for specific errors and handle them differently, or try to create a different path. Perhaps let the user choose a different one?
– e.James
May 25 '11 at 15:17
I'm not sure what to do if there is an error. It depends how fancy you want to get. You could look for specific errors and handle them differently, or try to create a different path. Perhaps let the user choose a different one?
– e.James
May 25 '11 at 15:17
5
5
You need to check the return value, not the error pointer
– Sven
Feb 9 '13 at 8:17
You need to check the return value, not the error pointer
– Sven
Feb 9 '13 at 8:17
1
1
@Sven: I don't see how it makes a difference to the logic?
– e.James
Feb 10 '13 at 14:53
@Sven: I don't see how it makes a difference to the logic?
– e.James
Feb 10 '13 at 14:53
Cocoa error handling generally does not promise that the error pointer will be
NULL
on success, only that it will be populated on failure.– Josh Caswell
Oct 21 '17 at 19:56
Cocoa error handling generally does not promise that the error pointer will be
NULL
on success, only that it will be populated on failure.– Josh Caswell
Oct 21 '17 at 19:56
add a comment |
For Swift 3.0
do {
try FileManager.default.createDirectory(atPath: folder, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
add a comment |
For Swift 3.0
do {
try FileManager.default.createDirectory(atPath: folder, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
add a comment |
For Swift 3.0
do {
try FileManager.default.createDirectory(atPath: folder, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
For Swift 3.0
do {
try FileManager.default.createDirectory(atPath: folder, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
answered Nov 26 '16 at 10:28


Sergey NikitinSergey Nikitin
637522
637522
add a comment |
add a comment |
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.
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.
What do you mean by "flush the file system"?
– dontWatchMyProfile
May 25 '11 at 15:02
dontWatchMyProfile: Writes to a disk (or in this case, FLASH storage) are often first written to much faster RAM, then committed to main storage as performance and load allow. Flushing that cache, or "syncing", commands the system to "write the entire contents of the cache right now." This is important in case, as the OP says, power is lost. I have little information regarding file IO on the iPhone and related devices, but a hopefully relevant SO thread exists here: stackoverflow.com/questions/459537/…
– Charles Burns
May 25 '11 at 16:58
2
Between line two and line three you should add a comment like this: // Right here, we switch processes and someone else can create a file with the name we just passed to fileExistsAtPath:isDirectory: - so this check isn't really worth doing.
– James Moore
Nov 29 '12 at 18:48
add a comment |
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.
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.
What do you mean by "flush the file system"?
– dontWatchMyProfile
May 25 '11 at 15:02
dontWatchMyProfile: Writes to a disk (or in this case, FLASH storage) are often first written to much faster RAM, then committed to main storage as performance and load allow. Flushing that cache, or "syncing", commands the system to "write the entire contents of the cache right now." This is important in case, as the OP says, power is lost. I have little information regarding file IO on the iPhone and related devices, but a hopefully relevant SO thread exists here: stackoverflow.com/questions/459537/…
– Charles Burns
May 25 '11 at 16:58
2
Between line two and line three you should add a comment like this: // Right here, we switch processes and someone else can create a file with the name we just passed to fileExistsAtPath:isDirectory: - so this check isn't really worth doing.
– James Moore
Nov 29 '12 at 18:48
add a comment |
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.
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.
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.
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.
edited May 23 '17 at 11:55
Community♦
11
11
answered May 25 '11 at 14:27
Charles BurnsCharles Burns
7,48454872
7,48454872
What do you mean by "flush the file system"?
– dontWatchMyProfile
May 25 '11 at 15:02
dontWatchMyProfile: Writes to a disk (or in this case, FLASH storage) are often first written to much faster RAM, then committed to main storage as performance and load allow. Flushing that cache, or "syncing", commands the system to "write the entire contents of the cache right now." This is important in case, as the OP says, power is lost. I have little information regarding file IO on the iPhone and related devices, but a hopefully relevant SO thread exists here: stackoverflow.com/questions/459537/…
– Charles Burns
May 25 '11 at 16:58
2
Between line two and line three you should add a comment like this: // Right here, we switch processes and someone else can create a file with the name we just passed to fileExistsAtPath:isDirectory: - so this check isn't really worth doing.
– James Moore
Nov 29 '12 at 18:48
add a comment |
What do you mean by "flush the file system"?
– dontWatchMyProfile
May 25 '11 at 15:02
dontWatchMyProfile: Writes to a disk (or in this case, FLASH storage) are often first written to much faster RAM, then committed to main storage as performance and load allow. Flushing that cache, or "syncing", commands the system to "write the entire contents of the cache right now." This is important in case, as the OP says, power is lost. I have little information regarding file IO on the iPhone and related devices, but a hopefully relevant SO thread exists here: stackoverflow.com/questions/459537/…
– Charles Burns
May 25 '11 at 16:58
2
Between line two and line three you should add a comment like this: // Right here, we switch processes and someone else can create a file with the name we just passed to fileExistsAtPath:isDirectory: - so this check isn't really worth doing.
– James Moore
Nov 29 '12 at 18:48
What do you mean by "flush the file system"?
– dontWatchMyProfile
May 25 '11 at 15:02
What do you mean by "flush the file system"?
– dontWatchMyProfile
May 25 '11 at 15:02
dontWatchMyProfile: Writes to a disk (or in this case, FLASH storage) are often first written to much faster RAM, then committed to main storage as performance and load allow. Flushing that cache, or "syncing", commands the system to "write the entire contents of the cache right now." This is important in case, as the OP says, power is lost. I have little information regarding file IO on the iPhone and related devices, but a hopefully relevant SO thread exists here: stackoverflow.com/questions/459537/…
– Charles Burns
May 25 '11 at 16:58
dontWatchMyProfile: Writes to a disk (or in this case, FLASH storage) are often first written to much faster RAM, then committed to main storage as performance and load allow. Flushing that cache, or "syncing", commands the system to "write the entire contents of the cache right now." This is important in case, as the OP says, power is lost. I have little information regarding file IO on the iPhone and related devices, but a hopefully relevant SO thread exists here: stackoverflow.com/questions/459537/…
– Charles Burns
May 25 '11 at 16:58
2
2
Between line two and line three you should add a comment like this: // Right here, we switch processes and someone else can create a file with the name we just passed to fileExistsAtPath:isDirectory: - so this check isn't really worth doing.
– James Moore
Nov 29 '12 at 18:48
Between line two and line three you should add a comment like this: // Right here, we switch processes and someone else can create a file with the name we just passed to fileExistsAtPath:isDirectory: - so this check isn't really worth doing.
– James Moore
Nov 29 '12 at 18:48
add a comment |
In swift 2 it looks like this:
do {
try NSFileManager.defaultManager().createDirectoryAtPath(pathURL.absoluteString, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
add a comment |
In swift 2 it looks like this:
do {
try NSFileManager.defaultManager().createDirectoryAtPath(pathURL.absoluteString, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
add a comment |
In swift 2 it looks like this:
do {
try NSFileManager.defaultManager().createDirectoryAtPath(pathURL.absoluteString, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
In swift 2 it looks like this:
do {
try NSFileManager.defaultManager().createDirectoryAtPath(pathURL.absoluteString, withIntermediateDirectories: true, attributes: nil)
} catch {
print(error)
}
edited Nov 26 '16 at 12:11


Sergey Nikitin
637522
637522
answered Nov 29 '15 at 3:59


ChrisChris
1,83822126
1,83822126
add a comment |
add a comment |
Swift 4.2
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let imagesPath = documentsURL.appendingPathComponent("Images")
do
{
try FileManager.default.createDirectory(atPath: imagesPath.path, withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError
{
NSLog("Unable to create directory (error.debugDescription)")
}
add a comment |
Swift 4.2
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let imagesPath = documentsURL.appendingPathComponent("Images")
do
{
try FileManager.default.createDirectory(atPath: imagesPath.path, withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError
{
NSLog("Unable to create directory (error.debugDescription)")
}
add a comment |
Swift 4.2
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let imagesPath = documentsURL.appendingPathComponent("Images")
do
{
try FileManager.default.createDirectory(atPath: imagesPath.path, withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError
{
NSLog("Unable to create directory (error.debugDescription)")
}
Swift 4.2
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let imagesPath = documentsURL.appendingPathComponent("Images")
do
{
try FileManager.default.createDirectory(atPath: imagesPath.path, withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError
{
NSLog("Unable to create directory (error.debugDescription)")
}
answered Nov 20 '18 at 11:11
Abhishek JainAbhishek Jain
2,44621124
2,44621124
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6125819%2fis-there-a-safer-way-to-create-a-directory-if-it-does-not-exist%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown