2016-07-30 3 views
-1

私はswiftを使用しています。プログラムで作成されたディレクトリ内にファイルを表示するテーブルビューがあります。私は別のディレクトリ内にあるディレクトリ内にファイルを作成したい。ドキュメントディレクトリ内のディレクトリ1 /ディレクトリ2 /内のwriteToFile SWIFT

例 - にdirectory1/Directory2/fileIwantToCreate.pdf

私は、ドキュメントディレクトリにアクセスする方法を知っているが、私は、私はディレクトリ内のディレクトリにアクセスして行く方法などわかりません。前もって感謝します。

+0

@AMomchilov私は実際にそれを理解しました。私は単に文字列に別のディレクトリを追加する必要があります。例 - let Path = documentsDirectory.stringByAppendingPathComponent( "Path1/SubPath1 /") –

+0

これは私が疑ったものです。この質問へのあなたの答えを提出し、それを受け入れてください – Alexander

答えて

0

、私はあなたがpdfファイルを持っていることを、ここで仮定していますデータ。

特定のディレクトリ内のDocumentDirectoryにファイルを格納するには、ディレクトリが存在するかどうかを確認する必要があります(存在しない場合)。

var path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] 
do { 
    path = path.stringByAppendingFormat("/Dir1/Dir2") 
    if (!NSFileManager.defaultManager().fileExistsAtPath(path)) { 
     try NSFileManager.defaultManager().createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil) 
    } 
    //Now write the data of pdf file here 
    path = path.stringByAppendingFormat("/Filename.pdf") 
    let data = NSData() //Here data is NSData of your pdf file 
    try data.writeToFile(path, options: .DataWritingAtomic) 
} 
catch let error as NSError { 
    print(error.localizedDescription) 
} 
0

文字列に別のディレクトリを追加するだけです。

LETパス= documentsDirectory.stringByAppendingPathComponent( "パス1/SubPath1 /")

あなたのpdfファイルの NSDataを持っている必要があります DocumentDirectorywriteToFileについて
関連する問題