Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create directory using Swift code (NSFileManager)

I'm having some trouble with converting Objective-C code to create a directory for Swift.

Objective-C:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);     NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder     NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];      if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])     [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; 
like image 998
Gian Avatar asked Nov 14 '14 13:11

Gian


People also ask

How do I create a directory in Swift?

Swift – Create Directory at Specific Path To create a Directory at specific path in Swift, call createDirectory() function on the file manager object. The following code snippet creates Directory at path “/ab/cd/”. withIntermediateDirectories: true creates intermediate directories if not present.


2 Answers

Swift 5.0

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let documentsDirectory = paths[0] let docURL = URL(string: documentsDirectory)! let dataPath = docURL.appendingPathComponent("MyFolder") if !FileManager.default.fileExists(atPath: dataPath.path) {     do {         try FileManager.default.createDirectory(atPath: dataPath.path, withIntermediateDirectories: true, attributes: nil)     } catch {         print(error.localizedDescription)     } } 

Swift 4.0

let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) let documentsDirectory: AnyObject = paths[0] as AnyObject let dataPath = documentsDirectory.appendingPathComponent("MyFolder")!      do {     try FileManager.default.createDirectory(atPath: dataPath.absoluteString, withIntermediateDirectories: false, attributes: nil) } catch let error as NSError {     print(error.localizedDescription) } 

Swift 3.0

let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) let documentsDirectory: AnyObject = paths[0] let dataPath = documentsDirectory.appendingPathComponent("MyFolder")!          do {     try FileManager.default.createDirectory(atPath: dataPath.absoluteString, withIntermediateDirectories: false, attributes: nil) } catch let error as NSError {     print(error.localizedDescription) } 

Swift 2.1

You can create directory using below method:

let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) let documentsDirectory: AnyObject = paths[0] let dataPath = documentsDirectory.stringByAppendingPathComponent("MyFolder")  do {     try NSFileManager.defaultManager().createDirectoryAtPath(dataPath, withIntermediateDirectories: false, attributes: nil) } catch let error as NSError {     print(error.localizedDescription) } 
like image 140
Kampai Avatar answered Oct 01 '22 10:10

Kampai


None of @Kampai and @Crashalot's answers worked for me.

The .absoluteString makes a url with file:// prefix and it cause exception while creating directory. Instead I've used .path method.

The fixed code for Swift 3

let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let dataPath = documentsDirectory.appendingPathComponent("MyFolder")  do {     try FileManager.default.createDirectory(atPath: dataPath.path, withIntermediateDirectories: true, attributes: nil) } catch let error as NSError {     print("Error creating directory: \(error.localizedDescription)") } 
like image 25
Mehdi Hosseinzadeh Avatar answered Oct 01 '22 12:10

Mehdi Hosseinzadeh