151

iPhone アプリのドキュメント フォルダーに新しいフォルダーを作成したいだけです。

誰もそれを行う方法を知っていますか?

あなたの助けに感謝!

4

9 に答える 9

328

私は次のようにします:

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
于 2009-11-19T12:10:27.817 に答える
14

Manni の回答にコメントするほどの評判はありませんが、[paths objectAtIndex: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 用に設計されたもので、これらの各ディレクトリが複数存在する可能性があるため、単一のパスではなくパスの配列を返します。iOS では、結果の配列には、ディレクトリへの単一のパスが含まれている必要があります。リスト 3-1 は、この関数の典型的な使い方を示しています。

コード リスト 3-1 アプリケーションの Documents ディレクトリへのパスを取得する

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
于 2011-01-13T01:57:21.293 に答える
14

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)")
                }
            }
        }
    }
于 2016-12-04T12:30:35.600 に答える
9

"[paths objectAtIndex:0]" は好きではありません。Apple が "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
于 2011-01-03T14:05:03.203 に答える
8

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)")
        }

    }
于 2015-10-05T10:31:37.067 に答える
5

これは私にとってはうまくいきます、

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");
}
于 2013-04-11T17:47:06.707 に答える
1

次のコードは、ディレクトリの作成に役立つ場合があります。

-(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"];

結果 :

ディレクトリが作成されました!

データパス : /var/mobile/Applications/BD4B5566-1F11-4723-B54C-F1D0B23CBC/Documents/MyFolder

于 2014-01-24T14:41:40.720 に答える
1

スイフト 1.2 および iOS 8

ディレクトリが存在しない場合にのみ、ドキュメント ディレクトリ内にカスタム ディレクトリ (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...")
    }

}
于 2015-07-13T13:19:59.110 に答える