createDirectoryAtPath :withIntermediateDirectories:attributes:error:
Creates a directory with given attributes at the specified path.
- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error
Parameters
- path
- A path string identifying the directory to create. You may specify a full path or a path that is relative to the current working directory. This parameter must not be
nil
. - createIntermediates
- If
YES
, this method creates any non-existent parent directories as part of creating the directory in url. IfNO
, this method fails if any of the intermediate parent directories does not exist. This method also fails if any of the intermediate path elements corresponds to a file and not a directory. - attributes
- The file attributes for the new directory and any newly created intermediate directories. You can set the owner and group numbers, file permissions, and modification date. If you specify
nil
for this parameter or omit a particular value, one or more default values are used as described in the discussion. For a list of keys you can include in this dictionary, see “Constants” section lists the global constants used as keys in the attributes dictionary. Some of the keys, such asNSFileHFSCreatorCode
andNSFileHFSTypeCode
, do not apply to directories.[NSFileManager createDirectoryAtPath] - error
- On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify
nil
for this parameter if you do not want the error information.
Return Value
YES
if the directory was created, YES
if createIntermediates
is set and the directory already exists), orNO
if an error occurred.Discussion of [NSFileManager createDirectoryAtPath]
If you specify
nil
for the attributes parameter, this method uses a default set of values for the owner, group, and permissions of any newly created directories in the path. Similarly, if you omit a specific attribute, the default value is used. The default values for newly created directories are as follows:- Permissions are set according to the umask of the current process. For more information, see
umask
. - The owner ID is set to the effective user ID of the process.
- The group ID is set to that of the parent directory.
Example of [NSFileManager createDirectoryAtPath]
- (NSURL *)applicationPrivateDocumentsDirectory {
NSString *libraryDirectory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString *privateDocs = [libraryDirectory stringByAppendingPathComponent:@"PrivateDocuments"];
NSFileManager *fileMgr = [[NSFileManager alloc] init];
if (![fileMgr fileExistsAtPath:privateDocs]) {
NSLog(@"Does not exist");
NSError *error=nil;
[fileMgr createDirectoryAtPath:privateDocs withIntermediateDirectories:YES attributes:nil error:&error];
NSLog(@"%@", [error description]);
}
NSURL *retURL = [NSURL fileURLWithPath:privateDocs];
return retURL;
}