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];
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.
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) }
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)") }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With