在iOS应用程序中创build文档文件夹内的文件夹

我只想在我的iPhone应用程序的文档文件夹中创build新的文件夹。

有人知道如何做到这一点?

感谢你的帮助!

我这样做:

NSError *error; 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]; //Create folder 

我没有足够的信誉来评论曼尼的答案,但[pathobjectAtIndex:0]是获取应用程序的文档目录

http://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StandardBehaviors/StandardBehaviors.html#//apple_ref/doc/uid/TP40007072-CH4-SW6

由于NSSearchPathForDirectoriesInDomains函数最初是为Mac OS X而devise的,因为这些目录中可能有多个目录,所以它将返回一个path数组而不是单个path。 在iOS中,结果数组应包含目录的单个path。 清单3-1显示了这个函数的典型用法。

清单3-1获取应用程序的Documents目录的path

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; 

我不喜欢“[paths objectAtIndex:0]”,因为如果苹果添加一个以“A”,“B”或“C”开头的新文件夹,“Documents”文件夹不是目录中的第一个文件夹。

更好:

 NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/MyFolder"]; if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder 

Swift 2解决scheme:

 let documentDirectoryPath: String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! if !NSFileManager.defaultManager().fileExistsAtPath(documentDirectoryPath) { do { try NSFileManager.defaultManager().createDirectoryAtPath(documentDirectoryPath, withIntermediateDirectories: false, attributes: nil) } catch let createDirectoryError as NSError { print("Error with creating directory at path: \(createDirectoryError.localizedDescription)") } } 

Swift 3解决scheme:

 private func createImagesFolder() { // path to documents directory let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first if let documentDirectoryPath = documentDirectoryPath { // create the custom folder path let imagesDirectoryPath = documentDirectoryPath.appending("/images") let fileManager = FileManager.default if !fileManager.fileExists(atPath: imagesDirectoryPath) { do { try fileManager.createDirectory(atPath: imagesDirectoryPath, withIntermediateDirectories: false, attributes: nil) } catch { print("Error creating images folder in documents dir: \(error)") } } } } 

这对我来说很好,

 NSFileManager *fm = [NSFileManager defaultManager]; NSArray *appSupportDir = [fm URLsForDirectory:NSDocumentsDirectory inDomains:NSUserDomainMask]; NSURL* dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:@"YourFolderName"]; NSError* theError = nil; //error setting if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES attributes:nil error:&theError]) { NSLog(@"not created"); } 

以下代码可能有助于创build目录:

 -(void) createDirectory : (NSString *) dirName { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory dataPath = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:dirName]; NSError *error; if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]) { NSLog(@"Couldn't create directory error: %@", error); } else { NSLog(@"directory created!"); } NSLog(@"dataPath : %@ ",dataPath); // Path of folder created } 

用法:

 [self createDirectory:@"MyFolder"]; 

结果:

创build的目录!

dataPath:/ var / mobile / Applications / BD4B5566-1F11-4723-B54C-F1D0B23CBC / Documents / MyFolder

Swift 1.2和iOS 8

在文档目录中创build自定义目录(name =“MyCustomData”),但前提是该目录不存在。

 // path to documents directory let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String // create the custom folder path let myCustomDataDirectoryPath = documentDirectoryPath.stringByAppendingPathComponent("/MyCustomData") // check if directory does not exist if NSFileManager.defaultManager().fileExistsAtPath(myCustomDataDirectoryPath) == false { // create the directory var createDirectoryError: NSError? = nil NSFileManager.defaultManager().createDirectoryAtPath(myCustomDataDirectoryPath, withIntermediateDirectories: false, attributes: nil, error: &createDirectoryError) // handle the error, you may call an exception if createDirectoryError != nil { println("Handle directory creation error...") } } 

Swift 3.0

 var error: Error? var paths: [Any] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) var documentsDirectory: String = paths[0] as? String ?? "" // Get documents folder var dataPath: String = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("/MyFolder").absoluteString if !FileManager.default.fileExists(atPath: dataPath) { try? FileManager.default.createDirectory(atPath: dataPath, withIntermediateDirectories: false, attributes: nil) }