Saya hanya ingin membuat folder baru di folder dokumen aplikasi iPhone saya.
Adakah yang tahu cara melakukannya?
Hargai bantuan Anda!
Saya hanya ingin membuat folder baru di folder dokumen aplikasi iPhone saya.
Adakah yang tahu cara melakukannya?
Hargai bantuan Anda!
Jawaban:
Saya melakukannya dengan cara berikut:
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
Saya tidak memiliki reputasi yang cukup untuk mengomentari jawaban Manni, tetapi [paths objectAtIndex: 0] adalah cara standar untuk mendapatkan Direktori Dokumen aplikasi tersebut
Karena fungsi NSSearchPathForDirectoriesInDomains awalnya dirancang untuk Mac OS X, di mana mungkin ada lebih dari satu dari masing-masing direktori ini, ia mengembalikan array jalur daripada jalur tunggal. Di iOS, array yang dihasilkan harus berisi jalur tunggal ke direktori. Listing 3-1 menunjukkan penggunaan khas dari fungsi ini.
Listing 3-1 Mendapatkan path ke direktori Documents aplikasi
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [@"~/Documents" stringByExpandingTildeInPath];
Solusi Swift 3:
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)")
}
}
}
}
Saya tidak suka "[paths objectAtIndex: 0]" karena jika Apple menambahkan folder baru dimulai dengan "A", "B" oder "C", folder "Documents" -folder bukan folder pertama dalam direktori.
Lebih baik:
NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/MyFolder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
Solusi Swift 2 :
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)")
}
}
Ini berfungsi baik untuk saya,
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");
}
Cepat 4.0
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
// Get documents folder
let documentsDirectory: String = paths.first ?? ""
// Get your folder path
let dataPath = documentsDirectory + "/yourFolderName"
if !FileManager.default.fileExists(atPath: dataPath) {
// Creates that folder if not exists
try? FileManager.default.createDirectory(atPath: dataPath, withIntermediateDirectories: false, attributes: nil)
}
Kode berikut dapat membantu dalam membuat direktori:
-(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
}
Penggunaan:
[self createDirectory:@"MyFolder"];
Hasil:
direktori dibuat!
dataPath: / var / mobile / Applications / BD4B5566-1F11-4723-B54C-F1D0B23CBC / Dokumen / MyFolder
Swift 1.2 dan iOS 8
Buat direktori khusus (nama = "MyCustomData") di dalam direktori dokumen tetapi hanya jika direktori tidak ada.
// 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...")
}
}